Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I find out the direction of movement of an object/part?

Asked by 4 years ago

The problem is I don't know how to get the direction of movement. Is it a hidden property of a part? Or a function? Or should I calculate it somehow? I have not found my answer for my question in the internet.

2
CFrame.LookVector DeceptiveCaster 3761 — 4y

1 answer

Log in to vote
1
Answered by
Rare_tendo 3000 Moderation Voter Community Moderator
4 years ago

While CFrame.LookVector is an option, it doesn't necessarily give the direction an object is moving if it doesn't rotate to face the direction where it's moving, nor does it tell whether or not the body is moving.

You can get the Unit vector of the object's Velocity to get the direction of movement:

local vel = object.Velocity
local direction

if vel.Magnitude == 0 then
    direction = vel -- We can't get the unit vector of (0, 0, 0) where the body isn't moving, so we just return the same vector
else
    direction = vel.Unit
end
0
As I understand velocity.Magnitude is a speed, but what is Velocity.Unit, can you explain it? I can't find docs for that MrSuperKrut 167 — 4y
1
The unit vector describes the direction of the given finite vector. The unit vector doesn't really exist if the body which the vector represents isn't moving. This is the equivalent of the magnitude vector being 0, since magnitude can describe velocity as well as distance. DeceptiveCaster 3761 — 4y
1
To add on to what DeceptiveCaster has said, unit vectors have a magnitude of 1 Rare_tendo 3000 — 4y
Ad

Answer this question