How do I change a parts velocity in the direction that the character is facing?

I want to push a part out in a random direction in front of the character within a certain radius, and that radius always stays the same based on what direction the character is facing.

It kind of does it, but the radius changes if the character rotates

local RandomNumber = math.random(-500,500) --Not a set number
local RandomNumber2 = math.random(100,500) --Not a set number
local RandomNumber3 = math.random(100,500) --Not a set number

Part.Velocity = script.Parent.CFrame.LookVector * Vector3.new(RandomNumber,RandomNumber2,RandomNumber3)

Do you mean you want it to choose a random vector within a cone?

Are you, perhaps, adding random spread to a gun?

No

As an example I’ll use say a gun

With a gun, if the bullet shoots out in front of your character, I want to add in bullet spread so it’s random, but it has to stay in front of the character

My current issue is that it just doesn’t work properly. That radius changes based on the characters rotation/direction, and I don’t want that. You don’t want the gun to be shooting behind you if your character is looking a certain way.

i didn’t test this but maybe this is what you’re looking for?

local spread = 3
bullet.AssemblyLinearVelocity = CFrame.new(humanoidrootpart.Position, humanoidrootpart.CFrame * CFrame.new(math.random(-spread, spread), math.random(-spread, spread), -5)).LookVector * 100

spread variable isn’t a specific angle input

invalid argument #2 to ‘new’ (Vector3 expected, got CFrame)

You’re saying “radius”, but I think you want a cone. Radius isn’t quite specific enough in this context.

i.e. if your LookVector is the black line in this image, you want to pick some velocity in the red cone:

image

I borrowed that image from this stackoverflow answer.

Here’s the top answer on that question translated into lua:

-- axis: center line of cone
-- angle: angle from center to edge of cone
-- returns: a random unit vector inside the cone, evenly distributed along the partial surface of a unit sphere.
local function RandomCone(axis: Vector3, angle: number)
	local cosAngle = math.cos(angle)
	local z = 1 - math.random()*(1 - cosAngle)
	local phi = math.random()*math.pi*2
	local r = math.sqrt(1 - z*z)
	local x = r * math.cos(phi)
	local y = r * math.sin(phi)
	local vec = Vector3.new(x, y, z)
	if axis.Z > 0.9999 then
		return vec
	elseif axis.Z < -0.9999 then
		return -vec
	end
	local orth = Vector3.zAxis:Cross(axis)
	local rot = math.acos(axis:Dot(Vector3.zAxis))
	return CFrame.fromAxisAngle(orth, rot) * vec
end

You’d use it like

-- angle of the cone, i.e. the largest angle off the firing axis you could be
local SPREAD = math.rad(45)

-- speed of the bullet (can also be random if you want)
local SPEED = 100

part.AssemblyLinearVelocity = RandomCone(script.Parent.CFrame.LookVector, SPREAD) * SPEED

Haven’t tested this yet. If this is not what you’re looking for please elaborate a bit more :slight_smile:

32 Likes

This is what I am looking for, but there are a few errors, so I can’t use the script.

The ‘dir’ and ‘Vector’ are undefined. Can you please test and fix this?

Fixed up the typos and edited my reply.

You didn’t? It’s still the same as before

1 Like

My bad. Edited for real this time :slight_smile:

Thanks a bunch!!! You’re the best :slight_smile:

Great solution but thought I’d share mine as its more simple and allows different spread in X and Y:

		local random = Random.new()
		local function randomPointInEllipse(spreadDegrees : Vector2)
			local angle = random:NextNumber(0, math.pi * 2)
			local u = random:NextNumber() + random:NextNumber()
			local r = (u > 1 and 2 - u or u)
			local spreadRadians = Vector2.new(math.rad(spreadDegrees.Y), math.rad(spreadDegrees.X))
			return Vector2.new(
				r * math.cos(angle) * spreadRadians.X,
				r * math.sin(angle) * spreadRadians.Y
			)
		end
		
		local ang = randomPointInEllipse(spread)
		local bulletdirectioncf =  CFrame.lookAt(bulletComeFromPos, rayHitPosition)
		local bulletdirection = (bulletdirectioncf * CFrame.Angles(ang.X,ang.Y, 0)).LookVector

2 yeEEAAARRRSSS LATER!!! But thank you, I’m sure this will help the future people who view this thread

1 Like