I am using the following code to setup my rotations:
As you can see I'm using 'Quaternion.LookRotation( direction.normalized )' to calculate the rotation towards the new target, but this only works on 'transform.forward' and my vehicle is moving on the x axis (transform.right) instead of the z axis, which is forward.
That's why i do 'newRotation.eulerAngles.y - 90' in the angle calculation. I would rather not do that, and I also don't want to child the vehicle to a parent which faces forward, so I was wondering if there is any alternative to Quaternion.LookRotation() that i can use, which doesn't force the rotation to the z axis?
Thanks for your time guys! Any help is greatly appreciated :)
asked May 01 '12 at 06:02 AM
Found the solution, here is an alternative to using Quaternion.LookRotation():
The Mathf.Atan2 function return an angle from 2 numbers (* Mathf.Rad2Deg is necessary to convert the angle to degrees), which is all I needed! Then I just create a smooth rotation from my vehicle's current y rotation to the new angle, and apply the results to "rigidbody.MoveRotation()".
Only thing I had to do for it to work correctly was to use '-turnAngle' instead of 'turnAngle' for the rotation to happen in the correct direction.
That's it! Maybe this will be helpful to others :)
answered May 01 '12 at 07:41 PM
There's an overload for LookRotation that allows you to specify the 'Up' vector. Just change that line to
and you should be sorted.
answered May 01 '12 at 06:50 AM