- Home /
Velocity based on rotation
Hi so im trying to take a gameobject and make its velocity follow whatever rotation it has.
Ex: its at a 45 degree angle it travels diagonal , its rotation is 90 so it travels horizontal etc.
right now my code only make is so that all things travel to the up ( because of transform.forward), is there a simple solution to my problem? thanks
var rot : Quaternion;
rot = Quaternion.Euler(0, player.transform.rotation.y,0);
transform.rotation = rot;
rigidbody.velocity = transform.forward * speed;
Is this script assigned to the "player" gameobject? If not, then player.transform.forward should always be forward relative to it's rotation.
Thanks a ton Scribe that was the problem, now it works perfectly!!!
Answer by Scribe · Jan 19, 2015 at 04:17 PM
I think this line: rot = Quaternion.Euler(0, player.transform.rotation.y,0);
should be:
rot = Quaternion.Euler(0, player.transform.eulerAngles.y,0);
You are welcome! Just to add the reason, this is because player.transform.rotation
is of type Quaternion which is a more rigorous way of representing angles with 4 components and it NOT what is shown in the inspector.
eulerAngles is the Eulerian representation of the angle and what you see in the inspector, which has some problems in computer graphics for lerping etc due to gimbal lock and ambiguity, but are much easier to imagine and understanding.
It appears to be quite a common mistake/misunderstanding by those unfamiliar with unity!
Yeah im pretty new to it ive only been using it for like a month now but thats really good info to know! Thanks again!!!
Your answer
Follow this Question
Related Questions
Bullet rifling simulation 1 Answer
Rigidbody Turning 2 Answers
Can you add a force to a rigidbody at an angle? 3 Answers
Can i Move/Rotate triggers without Rigidbodies? And other collider questions. 3 Answers
Pysics not working as expected 1 Answer