I am not sure if there’s a formula or just a simple function but how can you tell how fast a object is going with the velocity? Is there a function that you can apply to a vector to find out or is it just by dividing and multiplying the velocity and/or the orientation?
local object = workspace.MovingPart -- your object here
print(object.Velocity.magnitude)
To expand a bit, a vector is made up of a direction and a length.
To get the speed, you need to get the length. The length is also called the magnitude.
The individual X, Y, and Z components of a Vector3 describe the “speed” on the X, Y, and Z axes. Every second you will move by new_position = current_position + velocity
, where each X, Y, and Z component will be added to the X, Y, and Z component of current_position
.
If you recognize the distance formula ( sqrt((x2 - x1)^2 + (y2 - y1)^2)
), then you’ll recognize the formula for magnitude. Velocity already represents a change (e.g. x2 - x1
) so we can just do sqrt(x^2 + y^2)
. It’s easy to expand this to 3d: sqrt(x^2 + y^2 + z^2)
. All we’re doing is finding the distance-from-zero – or length – of the vector.
velocity.magnitude == math.sqrt(velocity.x^2 + velocity.y^2 + velocity.z^2)
Don’t forget to mark @dragonfrosting’s answer as the solution.
I am not that old and am barley getting into algebra so none of that made sense.
I can’t change if though. How do I change it. Or how to I get the velocity from the speed?
Speed and velocity are nearly the same thing, but speed is a scalar measurement and velocity is a vector measurement. This means that velocity provides a direction on top of the rate of change displacement, in Roblox, it’s split up into 3 components: X, Y and Z which each describe the number of studs per second the object is moving on each axis.
Without extra information provided with the speed of an object, it’s impossible to get a velocity value from it. It’d be like me asking someone if I was travelling at 120 km/h down a road, which way was I heading? They’d have no clue, so getting the velocity value from speed is impossible. The best you could do is just have all the speed transferred to either the X, Y or Z axis using: obj.Velocity = Vector3.new(speed, 0, 0)
TL;DR: No way of getting velocity from speed because it requires a direction.
If you are able to get another direction (vector/velocity), then you can create a vector (velocity) heading in that direction with the length (speed) you want.
For example, if you want a vector (velocity) heading in the direction of where the camera is looking at 50 units per second, you can use velocity = workspace.CurrentCamera.CFrame.lookVector*50
If you want a velocity facing point B from point A at 50 units per second, then you first need to get a vector (velocity) heading in that direction then change its length (speed) to 50.
Velocity is fundamentally a change in position. If you get the change in position from point A to point B, then you already have a velocity, it’s just not the right speed!
- Get the change in position to get a velocity:
velocity = partB.Position - partA.Position
- Make the velocity 1 unit per second:
velocity = velocity.unit
- Multiply the 1 u/s velocity by 50 to get 50 u/s:
velocity = velocity*50
You can smash these all together to get velocity = (partB.Position - partA.Position).unit*50
That is a velocity heading towards part B from part A at 50 units per second.
Anyways, like @General_Scripter says, you can’t get a velocity with just speed. You need a direction and a speed to get velocity. A direction is just an existing vector/velocity, which you can change to be the speed you want with some math. Both of these examples get a 1 unit per second vector/velocity first, then multiply it by the speed we want to get a velocity heading in the direction we want with a speed we want.
I just want to make a max speed for my car.
If you believe working with velocity is the way to go:
local function capSpeed(vel, maxSpeed)
local speed = vel.magnitude
if speed > maxSpeed then
return vel.unit * maxSpeed
else
return vel
end
end
Alternativly you could just take the min(speed, maxSpeed) and multiply instead of using if/else.
Should I use a BodyThrust, a RocketPropulsion, a BodyVelocity, or just the parts velocity?
I haven’t worked with vehicles so I wouldn’t know. But for sure I can tell you RocketPropulsion is not for this situation. If you’re not using a BodyObject, velocity might be a good option.