Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Lerp Rotation with Quaternion.Euler not working.

Discussion in 'Scripting' started by psyhova, Jun 19, 2015.

  1. psyhova

    psyhova

    Joined:
    Oct 3, 2013
    Posts:
    32
    Hello Comunity

    I wrote this Script put it does not work correctly. I want to lerp a rotation to a local Vector.

    The script is attached to the camera and if you rotate with vert and hori Axes the cam rotates.
    The white line is the target Vector. if you press space the Quaternions goes to zero, except the z. Z is the main direction and thats ok. But if the yellow line (this is the main direction) is over hanging and you press space, he goes the false way round. (opposite direction of white)

    I you press b the yellow line always goes to the blue line which represents the transform.forward. There the LerpRotation doesent seem to accept the Quaternion Euler(aaa)

    Why is it alway going to the transform.forward?

    it really drives me crazy.

    Thanx for advices, tips and ideas.

    #pragma strict


    var aaa :Vector3;

    var turn : float;

    var speed : float= 10;



    function Start () {


    }


    function Update () {


    //Relative Vektoren

    Debug.DrawRay(transform.position,transform.forward,Color.yellow);
    Debug.DrawRay(transform.position,transform.up,Color.cyan);
    Debug.DrawRay(transform.position,transform.right,Color.magenta);

    //Welt Wektoren

    Debug.DrawRay(transform.position,Vector3.forward,Color.blue);
    Debug.DrawRay(transform.position,Vector3.up,Color.green);
    Debug.DrawRay(transform.position,Vector3.right,Color.red);
    Debug.DrawRay(transform.position,Vector3(transform.forward.x,0,transform.forward.z),Color.white);
    Debug.DrawRay(transform.position+transform.forward,Vector3.up,Color.black);
    if (Input.GetKey("b")==true)
    {
    aaa = Vector3(transform.forward.x,0,transform.forward.z);
    print("AAA"+aaa);
    var bbb = Quaternion.Euler(aaa);
    transform.rotation=Quaternion.Lerp(transform.rotation,Quaternion.Euler(aaa.x,0,aaa.z),0.5);
    //transform.rotation=Quaternion.Lerp(transform.rotation,Quaternion.Euler(aaa),0.5);
    }

    }
    if (Input.GetKey("space")==true)
    {

    print("fffffffff");

    var xxx = transform.rotation;
    xxx.x=0;
    xxx.z=0;

    transform.rotation=Quaternion.Lerp(transform.rotation,xxx,0.2);
    }
    transform.Rotate(Input.GetAxis("Vertical"),Input.GetAxis("Horizontal")*turn,0);
    }
     
  2. unitychristy2

    unitychristy2

    Joined:
    Sep 8, 2013
    Posts:
    38
    Have you tried LocalEuler? instead of Euler
     
  3. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
  4. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    rotation.x and rotation.z are not what you think they are when it comes to Quaternions. Your lerp is also all wrong.

    Check out Quaternion.RotateTowards, it looks better suited for what you're trying to do.