Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I aim BodyVelocity to a point?

How can I set an object's bodyvelocity so it moves towards a set point?

So let's say I want a part to move from its spot at (0,0,0) to (10,0,1) with BodyVelocity. I insert it into the part. And the velocity of that part would be ?

2 comments

0

You can use BodyPosition to move a Part to a specific location in space. If you use BodyVelocity, then you'll have to turn it off when it's near the destination, which is a lot more inconvenient to program.

0

In this case, it would be easier to turn BodyVelocity off than to use BodyPosition. I appreciate the suggestion though.

1 answer

✓ Accepted answer

This is a simple vector problem. Using vector subtraction, you can derive the vector pointing from one vector to another vector.

pointingVector = endVector - startVector

where pointingVector is the vector pointing from startVector to endVector. This will be the direction of your velocity vector.

The magnitude of a velocity vector describes its speed. We know this because speed is defined to be the square root of the dot product of a velocity vector and itself. If we want a velocity to be a certain speed, we would first make it a unit vector by dividing the vector by its own magnitude and then multiplying it by the target speed. We can use the .unit property to make a vector a unit vector without the division.

Taking the above, setting the velocity of a part moving from (0, 0, 0) to (10, 0, 1) would look like the code below, assuming bodyVelocityreferences the BodyVelocity object.

local startPosition 	= Vector3.new(0, 0, 0)
local endPosition 		= Vector3.new(10, 0, 1)
local targetSpeed 		= 50 

bodyVelocity.velocity 	= targetSpeed*(endPosition - startPosition).unit

1 comment

0

Perfect. Clear, concise, exactly what I needed. Thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.