- Home /
 
how to set a velocity on a rigidbody based on its rotation;
In my project it Instantiates a gameobject at a random rotation, then it needs to set its velocity so it goes forward no matter what direction its facing, is there something like local velocity I can use?
Answer by N-8-D-e-v · Aug 27, 2020 at 11:05 PM
You can do some simple vector math, first, get a vector3 with the direction of the gameobject's transform like so
 Vector3 dir = transform.eulerAngles;
 
               Then, normalize it to get a directional vector, I recommend that you watch this if you don't already understand normalization
 dir = dir.normalized;
 
               then finally, just apply that to your velocity, with a float as the scalar (think of the scalar as your speed)
 float speed = 10;
 rb.velocity = dir * speed;
 
               And there you have it! If you have any questions on this or anything else really, don't hesitate to ask
Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Transferring velocity from kinematic to a rigid body 0 Answers
2d kinematic projectile velocity problem 0 Answers
How to compare velocity of joint between 2 model in Unity ? 0 Answers