The problem I have is a little different than what I have read about concerning the same issue. I found plenty of answers showing how to only rotate on 1 axis when using functions like ‘Quaternion.LookRotation()’, and I can achieve that just fine. My problem is that the object I am rotating towards the target is a motorcycle, which also rotates on the X axis for forward/backward pitch.
When I limit the rotation to the y axis only, and zero out the other 2, my motorcycle rotates towards the target on Y just fine, but it’s X axis rotation is now locked to 0…so when going up-hill or down-hill, the bike doesn’t rotate anymore.
If I don’t zero out the X axis for the rotation, the the bike’s X axis rotation is also following the target, so if my bike is going up-hill and the next waypoint is at y = 0 (on the ground), then the bike’s X rotation tries to point towards the waypoint and rotates the opposite way of the up-hill, down-hill.
I have hit a roadblock trying to figure out how to leave the bike’s X rotation alone while making its Y axis follow waypoints. Here’s what I have so far:
void Update ()
{
// Loop through waypoints
if( currentWaypoint < waypoints.Length )
{
// 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 );
Quaternion rot = Quaternion.Slerp( transform.rotation, newRotation,
transform.parent.rigidbody.velocity.magnitude * Time.deltaTime );
transform.rotation = rot;
// This version below gives me the bike's X rotation bike, but I gimbal lock problems...
//transform.rotation = Quaternion.Euler( transform.eulerAngles.x, rot.eulerAngles.y, 0.0f );
}
}
else
{
Debug.LogWarning("NO MORE WAYPOINTS TO FOLLOW");
}
}
Quaternions give me a headache I’ve been trying to get this to work for the past week, and although I am really close, it’s still not stable enough to be used.
If any of you rotation gurus out there can give me some much needed help, I would really appreciate it! Thanks for your time,
Stephane