How do I make rigidbody.velocity in local space?

I'm trying to make the velocity of an object into local space. All I know how to do is:

rigidbody.velocity = Vector3(0,0,yourVelocity)

Only, this makes the rigidbody move in GLOBAL z direction. Is there a way how to make Vector3 local? If there isn't way how to do that, can you tell me of another way how to move the rigidbody at a constant speed. Please give an example script in javascript.


You can use Transform.TransformDirection() to transform a vector in local space to world space. If you want the velocity to be parallel to one of the cardinal basis vectors in local space (as is the case in your example), you can use Transform.right, .up, or .forward.

For example, the code you posted would then look like this:

rigidbody.velocity = transform.forward * yourVelocity;

Although in this case 'yourVelocity' would probably be better named 'yourSpeed'.