Daniel Roca Lopez
Daniel Roca Lopez

Reputation: 23

Quaternion lookRotation - upwards doesn't work

I'm trying to make a bird rotate a bit "realistic", which is, vector up should point to the angle rotated, I mean, if you rotate 90º to your local right, vector up should now point -vector.forward (the one you had before rotating).

I tried with a function, but it only rotates sometimes, the rest, it just overwrites the vector.up as if it was 0,1,0.

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;

 public class SmoothPath : MonoBehaviour {

     [System.Serializable]
     public class TrackingSettings
     {
         [HideInInspector]
         public int currentPoint = 0;
         [HideInInspector]
         public int nextPoint = 1;
         public int pathToFollow = 0;
         public GameObject affectedObject;
         public float speed = 10;
         public float rotationSpeed = 12;
         [HideInInspector]
         public Quaternion lastRotation;
         [HideInInspector]
         public Vector3 lastLocation;
     }

     [System.Serializable]
     public class PathsToFollow
     {
         public GameObject pathReference;
         public Transform[] pathPoints;
     }

     public TrackingSettings[] objectsToTrack;
     private TrackingSettings tempObjectToTrack;
     private float timer = 0;

     public PathsToFollow[] pathsToFollow;

     private Quaternion lookRotation;
     private Transform currentNode;
     private Transform nextNode;
     private float distance;

     // Use this for initialization
     void Start()
     {
         foreach (TrackingSettings ts in objectsToTrack)
         {
             ts.currentPoint = 0;
             ts.nextPoint = 1;
             ts.affectedObject.transform.position = pathsToFollow[ts.pathToFollow].pathPoints[0].position;
             ts.lastRotation = ts.affectedObject.transform.rotation;
             ts.lastLocation = ts.affectedObject.transform.position;
             lookRotation = Quaternion.LookRotation(pathsToFollow[ts.pathToFollow].pathPoints[1].position - pathsToFollow[ts.pathToFollow].pathPoints[0].position);
             ts.affectedObject.transform.rotation = lookRotation;

             currentNode = pathsToFollow[ts.pathToFollow].pathPoints[ts.currentPoint];
             nextNode = pathsToFollow[ts.pathToFollow].pathPoints[ts.nextPoint];

             distance = Vector3.Distance(currentNode.position, nextNode.position);
         }
     }

     // Update is called once per frame
     void Update () {
         timer += Time.deltaTime;
         for (int i = 0; i < objectsToTrack.Length; i++)
         {
             tempObjectToTrack = objectsToTrack[i];

             if (Vector3.Distance(tempObjectToTrack.affectedObject.transform.position, nextNode.position) < 0.3f)
             {

                 tempObjectToTrack.currentPoint = tempObjectToTrack.nextPoint;
                 tempObjectToTrack.nextPoint++;
                 //reset if overflow
                 if (tempObjectToTrack.nextPoint == pathsToFollow[tempObjectToTrack.pathToFollow].pathPoints.Length) tempObjectToTrack.nextPoint = 0;

                 timer = 0;

                 //lastNode = pathsToFollow[tempObjectToTrack.pathToFollow].pathPoints[tempObjectToTrack.lastPoint];
                 currentNode = pathsToFollow[tempObjectToTrack.pathToFollow].pathPoints[tempObjectToTrack.currentPoint];
                 nextNode = pathsToFollow[tempObjectToTrack.pathToFollow].pathPoints[tempObjectToTrack.nextPoint];

                 distance = Vector3.Distance(currentNode.position, nextNode.position);

                 float angle = GetAngle(tempObjectToTrack.affectedObject.transform.forward, Vector3.Normalize(nextNode.position - currentNode.position));

                 //set last rotation and location
                 tempObjectToTrack.lastRotation = tempObjectToTrack.affectedObject.transform.localRotation; 
                 tempObjectToTrack.lastLocation = tempObjectToTrack.affectedObject.transform.position;

                 tempObjectToTrack.affectedObject.transform.up = new Vector3(angle / 2, 45, 0);
                 lookRotation = Quaternion.LookRotation(nextNode.position - tempObjectToTrack.affectedObject.transform.position, new Vector3(angle / 2, 45, 0));
                 Debug.Log("Up: " + new Vector3(angle / 2, 45, 0) + " - angle: " + angle);
                 //tempObjectToTrack.affectedObject.transform.localRotation = lookRotation;
             }

             tempObjectToTrack.affectedObject.transform.position = Vector3.SlerpUnclamped(tempObjectToTrack.lastLocation, nextNode.position, timer / distance * tempObjectToTrack.speed);

             tempObjectToTrack.affectedObject.transform.rotation = Quaternion.Slerp (tempObjectToTrack.lastRotation, lookRotation, timer / distance * tempObjectToTrack.rotationSpeed);


         }


     }

     private float GetAngle(Vector3 a, Vector3 b)
     {
         b.y = 0;
         a.y = 0;
         float angle = Vector3.Angle(a, b);
         float sign = Mathf.Sign(Vector3.Dot(Vector3.up, Vector3.Cross(a, b)));

         return angle * sign;
     }
 }

in a circle path (4 waypoints or more in squared way / round), the bird sometimes points properly and some others it doesn't

Upvotes: 1

Views: 839

Answers (1)

Ruzihm
Ruzihm

Reputation: 20249

I wouldn't use LookRotation for this because you don't know the direction you want local up to be outright. Here's an alternative using the angle directly to change the roll:

First, we clamp the result of GetAngle to +/-90f.

float angle = GetAngle(tempObjectToTrack.affectedObject.transform.forward, Vector3.Normalize(nextNode.position - currentNode.position));
angle  = Mathf.Clamp(angle,-90f,90f);

Then, we face the object looking towards the next goal, with local up being global up, then roll it by that amount by multiplying it by a rolling rotation:

lookRotation = Quaternion.LookRotation(nextNode.position - tempObjectToTrack.affectedObject.transform.position, Vector3.up);

// If the direction of roll isn't right, try `-1 * angle` here:

lookRotation = lookRotation * Quaternion.AngleAxis(angle, Vector3.forward);

Then, assign the Slerped lookRotation to the rotation:

tempObjectToTrack.affectedObject.transform.rotation = Quaternion.Slerp (tempObjectToTrack.lastRotation, lookRotation, timer / distance * tempObjectToTrack.rotationSpeed);

Upvotes: 1

Related Questions