Search Unity

How can i rotate the spaceship to the target direction ?

Discussion in 'Scripting' started by Chocolade, Sep 24, 2017.

  1. Chocolade

    Chocolade

    Joined:
    Jun 19, 2013
    Posts:
    933
    Not lookat but to rotate on Z axis only. The problem is that the spaceship is rotating on X and Z axis also some on Y and the spaceship is steering up.

    And i want it to rotate only on the Z axis.

    The script is attached to the spaceship. The script name is LookAtCamera but it's LookAtTarget but that is never mind i will change the name later.

    This is a screenshot of the spaceship on it's original position and rotation state:



    And this is a screenshot of the spaceship after the rotation. The target is a FPSController object i have down on the terrain. But instead rotating to it's direction on the Z the spaceship is steering up. I don't want to use LookAt i want to rotate the spaceship on Z to face the direction of the target it should move to.



    And this is a screenshot of how i want the spaceship to be rotating to the target this is what i mean on the Z (i think on the Z) but this is what i mean rotating the spaceship to face the target direction not lookat: I changed on my own the spaceship rotation on Z to -120 this is what i want the script to do to rotate on the Z facing the target direction:



    The script that is attached to the spaceship:

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class LookAtCamera : MonoBehaviour {
    7.  
    8.     //values that will be set in the Inspector
    9.     public Transform target;
    10.     public float RotationSpeed;
    11.  
    12.     //values for internal use
    13.     private Quaternion _lookRotation;
    14.     private Vector3 _direction;
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         //find the vector pointing from our position to the target
    20.         if (target)
    21.             _direction = (target.position - transform.position).normalized;
    22.  
    23.         //create the rotation we need to be in to look at the target
    24.         _lookRotation = Quaternion.LookRotation(_direction);
    25.  
    26.         //rotate us over time according to speed until we are in the required rotation
    27.         transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * RotationSpeed);
    28.     }
    29. }
    30.  
     
  2. Rob21894

    Rob21894

    Joined:
    Nov 21, 2013
    Posts:
    309
  3. JoshuaMcKenzie

    JoshuaMcKenzie

    Joined:
    Jun 20, 2015
    Posts:
    916
    The problem is that your model's wasn't exported with the expected axis orientation. Unity considers
    X = Right,
    Y = Up,
    Z = forward

    however your model is using
    X = Left
    Y = Foward
    X = Up

    You can still use LookRotation, just swap the variables.

    Code (CSharp):
    1. _lookRotation = Quaternion.LookRotation(Vector3.up, _direction);
     
    DanEliyahu likes this.