How do you get the speed of a VehicleSeat using a script?

Basicly I want to detect what speed a VehicleSeat is indicating / showing. But how do I accomplish this? Is there some kindof formula for calculating it? I am using a bodythrust to push the vehicle for those wondering.

5 Likes

It’s actually pretty simple, but I didn’t find out about this for a while.

First of all you want to get the Velocity of the seat. Then you want the Magnitude property of this as it will tell you the total speed of the seat.

I’m pretty sure that this is how it works internally although I could be wrong.

If you think about it, Velocity is a direction. So is a Unit vector which has a Magnitude of one. You can set the velocity to a Unit vector multiplied by a speed which multiplies the Magnitude.

1 Like

Velocity is a Vec3 value. So what parameter of the Velocity should I check the magnitude on? The X, or Y, or Z. (Or maybe I am not understanding you right?)

Vector3s and Vector2s have a Magnitude property which will calculate the magnitude of that vector.

You can find the distance between two objects using Magnitude. You’d subtract both positions and get the Magnitude of that vector.

If you want to know how Magnitude is calculated look up Pythagorean distance formula. It’s the same as (vecA-vecB).Magnitude.

2 Likes

So is it just Velocity.Magnitude?

1 Like

Yep… For example if you print your HumanoidRootPart’s Velocity.Magnitude while walking you’ll see it print your WalkSpeed.

10 Likes

Can Velocity.magnitude ever return negative? For example: the vehicle is going backwards so will Velocity.magnitude return negative?

No, Magnitude cannot return negative. Magnitude doesn’t tell you the direction it only tells you the “size” of the vector.

If you want Magnitude to be negative if the player goes backwards you can do this using Dot products (which aren’t actually too difficult to understand).

Edit (for the OP if he needs it or new viewers in the future):
a:Dot(b) will return how far in the direction of a that b travels.

So to get the forward/backward speed only you can do something like this: speed = vehicleSeat.CFrame.LookVector:Dot(vehicleSeat.Velocity)

This will return a negative value if the car is traveling backwards and positive if the car is traveling forwards but if the car is traveling perfectly straight left or right (so it isn’t going in the direction it’s facing at all) it will return 0. Aka how far the Velocity is traveling in the direction the seat is facing.

The value returned will be the speed since LookVector is a Unit vector. (If this had a Magnitude of 2 it would be the speed/2, 3 would be speed/3, etc)

14 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.