Alternative to Quaternion.LookRotation() ?

 0 I am using the following code to setup my rotations: ``````// Get direction of next waypoint Vector3 direction = waypoints[currentWaypoint].position - transform.position; // Check if we passed the current waypoint and get the next one if we did if( transform.position.x > waypoints[currentWaypoint].position.x ) { currentWaypoint++; } else { Quaternion newRotation = Quaternion.LookRotation( direction.normalized ); // Replace? float yAngle = Mathf.LerpAngle( transform.eulerAngles.y,newRotation.eulerAngles.y - 90, rBody.velocity.magnitude * Time.deltaTime ); rigidbody.MoveRotation( Quaternion.Euler( 0, yAngle, transform.eulerAngles.z )); } ``````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 :)Stephane more ▼ asked May 01 '12 at 06:02 AM ronronmx 804 ● 83 ● 99 ● 121 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 Found the solution, here is an alternative to using Quaternion.LookRotation(): ``````float turnAngle = Mathf.Atan2( direction.z, direction.x ) * Mathf.Rad2Deg; float smoothAngle = Mathf.LerpAngle( transform.eulerAngles.y, -turnAngle, rBody.velocity.magnitude * Time.deltaTime ); rigidbody.MoveRotation( Quaternion.Euler( 0, smoothAngle, transform.eulerAngles.z )); ``````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 :)Stephane more ▼ answered May 01 '12 at 07:41 PM ronronmx 804 ● 83 ● 99 ● 121 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users
 0 There's an overload for LookRotation that allows you to specify the 'Up' vector. Just change that line to ``````Quaternion newRotation = Quaternion.LookRotation( direction.normalized, transform.right); ``````and you should be sorted. more ▼ answered May 01 '12 at 06:50 AM syclamoth 14.8k ● 7 ● 15 ● 80 Thanks for the suggestion Kieren, but I've already tried that a few times and it doesn't work, since I need my rotation to happen on the up vector to turn the vehicle left/right, and if I change it to transform.right, it rotates the vehicle on the X axis and still tries to point the transform.forward axis towards the next target.Stephane May 01 '12 at 06:36 PM ronronmx add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x5056
x2155
x1780
x437
x134

asked: May 01 '12 at 06:02 AM

Seen: 1631 times

Last Updated: May 01 '12 at 07:41 PM