Search Unity

Quaternion.LookRotation relative the Map

Discussion in 'Scripting' started by Givago, Oct 20, 2017.

  1. Givago

    Givago

    Joined:
    Oct 22, 2015
    Posts:
    27
    Hi,

    I made a simple script for the obj look according to the direction I set.

    Code (CSharp):
    1.                 float moveHorizontal = player.GetAxis("Horizontal") * speed * Time.deltaTime;
    2.                 float moveVertical = player.GetAxis("Vertical") * speed * Time.deltaTime;
    3.                 Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    4.                 if (movement != Vector3.zero)
    5.                 {
    6.                     this.transform.rotation = Quaternion.LookRotation(movement);
    7.                 }
    But upon reaching a non-flat surface, he does not calculate relative to it. The problem is that I do not know how I make it look relative to the environment.

    //EDIT
    I took a look, what comes closest to what I want is the Rotate. But...
    It keeps spinning because I do not have any controllers, any suggestions?

    //EDIT 2
    NEW CODE
    Code (CSharp):
    1.                 float moveHorizontal = player.GetAxis("Horizontal") * speed * Time.deltaTime;
    2.                 float moveVertical = player.GetAxis("Vertical") * speed * Time.deltaTime;
    3.                 Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    4.                 if (movement != Vector3.zero)
    5.                 {
    6.                     Quaternion newRotation = Quaternion.LookRotation(movement);
    7.                     this.transform.rotation = Quaternion.Slerp(this.transform.rotation, newRotation, Time.deltaTime * 8);
    8.                 }
    still, it's a bit stuck, but if anyone has any suggestions I accept.
     
    Last edited: Oct 20, 2017