Search Unity

Quaternion.Slerp/Lerp: Rotating player 180 degrees and inverting z axis

Discussion in 'Scripting' started by KNR, Jan 29, 2015.

  1. KNR

    KNR

    Joined:
    Oct 9, 2014
    Posts:
    8
    Hey,

    In my project the 2d player object is turned by pressing a button. the player turns 180 degrees on y axis and inverted amount for zaxis.

    The problem i have is i am trying to rotate the player 180 degrees on the y axis. but i need to invert the z axis while im rotating the player.

    i have webplayer version of the problem im trying to figure out:
    http://home.tamk.fi/~e4tkaner/Rotateproblem/Rotateproblem.html

    As seen the bars with yellow prints in them the code rotates the gameobject 180 degrees but the z axis is not affected.
    I have been trying to multiply the axis.z value by -1 so it would be inverted but it hasnt really worked for me.

    The wanted rotation is the bars with blue lines, the wanted result would be that also the z axis would be rotated when button is pressed.

    Code:
    Code (CSharp):
    1. public static Vector3 pAxis;
    2.     private Quaternion right;
    3.     private Quaternion left;
    4.  
    5. //   public Quaternion left_i;
    6. //    private Quaternion left = Quaternion.Euler(0, 180, 0);
    7.  
    8.    
    9.     public float z;
    10.     public bool facingRight;
    11.     public bool buttonPressed;
    12.     public float time = 0.0f;
    13.  
    14.     void Start()
    15.     {
    16.         buttonPressed = false;
    17.         facingRight = true;
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.      //   left_i = Quaternion.Inverse(transform.rotation);
    23.         pAxis = transform.rotation.eulerAngles;
    24.         pAxis.x = 0;
    25.         SmoothFlip(time, facingRight);
    26.  
    27.         Debug.Log(pAxis.z.ToString());
    28.  
    29.         right = Quaternion.Euler(0, 0, pAxis.z);
    30.         left = Quaternion.Euler(0, 180, pAxis.z);
    31.      
    32.  
    33.         if (Input.GetKeyDown(KeyCode.Space))
    34.         {
    35.             Debug.Log("Turn");
    36.             buttonPressed = !buttonPressed;
    37.             time = 0.0f;
    38.             facingRight = !facingRight;          
    39.             pAxis.z = pAxis.z * -1;
    40.         }
    41.  
    42.     }
    43.  
    44.     void SmoothFlip(float time,bool facingRight)
    45.     {      
    46.         if (facingRight)
    47.         {
    48.             transform.rotation = Quaternion.Slerp(transform.rotation, right, Time.deltaTime);
    49.  
    50.         }
    51.         if (!facingRight)
    52.         {
    53.             transform.rotation = Quaternion.Slerp(transform.rotation, left, Time.deltaTime);
    54.         }
    55.     }
     
  2. meatpudding

    meatpudding

    Joined:
    Jan 28, 2015
    Posts:
    39
    You are only setting pAxis.z after you called SmoothFlip.
     
  3. KNR

    KNR

    Joined:
    Oct 9, 2014
    Posts:
    8
    Yes i figured that out eventually. too bad it didnt work quite as i hoped. Fixed the problem by inverting localscale.