Search Unity

Rotation problem - Change Roll of the mesh while yaw

Discussion in 'Scripting' started by deglet, Jul 17, 2017.

  1. deglet

    deglet

    Joined:
    Feb 4, 2016
    Posts:
    14
    Hello,
    I am making a simple plane game. When you press up and down you change the pitch; right and left the yaw.
    When the yaw change i would like the plane to slighty change is roll (visualy). So i made a parent with the script and a child with the model of the plane.

    This is how i yaw:


    Code (CSharp):
    1.  if (Input.GetAxis("horizontal") < -0.5)
    2.         {
    3.  
    4.             transform.Rotate(0, -15f * Time.deltaTime, 0);
    5.          
    6.             var target = Quaternion.Euler(0, 0, 45);
    7.          
    8.             PlaneMesh.transform.localRotation  = Quaternion.Lerp(PlaneMesh.transform.rotation, target, 0.5f);
    9.        
    10.         }
    11.         else
    12.         {
    13.             var target2 = Quaternion.Euler(0, 0, 0);
    14.             PlaneMesh.transform.localRotation = Quaternion.Lerp(PlaneMesh.transform.rotation, target2, 1f);
    15.         }
    But my quaternion.lerp don't work, the mesh yaw more than the parent and keep turning. If there is no lerp (1f) it behaves like i want but it's not smooth. Any idea ?
     
  2. deglet

    deglet

    Joined:
    Feb 4, 2016
    Posts:
    14
    found the solution; forgot the localrotation on Quaternion

    Code (CSharp):
    1.  if (Input.GetAxis("horizontal") < -0.5)
    2.         {
    3.  
    4.             transform.Rotate(0, -15f * Time.deltaTime, 0);
    5.  
    6.             //Faire Fn rotation pendant le Yaw
    7.          
    8.             var target = Quaternion.Euler(0, 0, 45);
    9.          
    10.             PlaneMesh.transform.localRotation  = Quaternion.Lerp(PlaneMesh.transform.localRotation, target, Time.deltaTime*1);
    11.          
    12.            // PlaneMesh.transform.localRotation = Quaternion.Euler(0, 0, 45);
    13.             //PlaneMesh.transform.Rotate(0, -1/2, 0);
    14.         }
    15.         else
    16.         {
    17.             var target2 = Quaternion.Euler(0, 0, 0);
    18.             PlaneMesh.transform.localRotation = Quaternion.Lerp(PlaneMesh.transform.localRotation, target2, Time.deltaTime * 1);
    19.         }