Search Unity

Quaternion.Slerp weirdness

Discussion in 'Scripting' started by lucidcoder, Apr 6, 2013.

  1. lucidcoder

    lucidcoder

    Joined:
    Mar 23, 2010
    Posts:
    138
    (Sidenote, made another version of this thread in Unity Support, ended up restructuring the code entirely to clean it up and ran into the same issue. Figured this forum would be more appropriate.)

    I am currently working on a small, space-themed mini strategy game. At the moment, I am implementing the portion of the AI that has the ships turn to face a waypoint once ordered to by the player, after which the ship will begin to move towards the designated waypoint. The problem exists with the ship facing the waypoint, that is to say, it doesn't face the ship.

    Here's an example of the problem, in video form since it's difficult to explain how it's behaving without just showing you: YouTube

    Here's the underlying code. The Quaternion.Slerp magic happens on line 40.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class UnitAI : MonoBehaviour {
    5.    
    6.     // Enable this to allow the unit to strategize for itself.
    7.     public bool autonomous = false;
    8.    
    9.     private Unit unit = null;
    10.     private Order[] queue = null;
    11.     private GameObject[] objects = null;
    12.     private Transform[] transforms = null;
    13.    
    14.     private bool timeForNextOrder = false;
    15.     private bool isRotated = false;
    16.    
    17.     // Use this for initialization
    18.     void Awake () {
    19.         unit = gameObject.GetComponent<Unit>();
    20.         queue = unit.queue;
    21.         objects = unit.gameobjectQueue;
    22.         transforms = unit.transformQueue;
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update () {
    27.         // Every frame we need to make sure this is up to date, this little block takes care of that.
    28.         queue = unit.queue;
    29.         objects = unit.gameobjectQueue;
    30.         transforms = unit.transformQueue;
    31.        
    32.         // First, we evaluate whether or not the unit needs to be doing anything.
    33.         if (queue[0] != Order.blank) {
    34.             // If we're here, the unit currently has an order at the top of its queue.
    35.             // Time to evaluate it.  Let's see what kind of order we were given.
    36.             switch (queue[0]) {
    37.                 case Order.waypoint:
    38.                     // If we're here, we've been told to go to a waypoint.
    39.                     if (isRotated == false) {
    40.                         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(transforms[0].position, Vector3.up), Time.deltaTime);
    41.                     }
    42.                 break;
    43.             }
    44.            
    45.             // If we're here, it's time for the next order to be evaluated, so let's delete it from the queue,
    46.             // reset the flag and go from there.
    47.             if (timeForNextOrder == true) {
    48.                 timeForNextOrder = false;
    49.                 unit.deleteFromQueue(0);
    50.             }
    51.            
    52.         } else {
    53.             // The queue is empty, put any variable resets you need in this block.
    54.             isRotated = false;
    55.         }
    56.     }
    57. }
    Any ideas?