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

Some help about rotating an object toward rigidbody velocity needed

Discussion in 'Scripting' started by plaguebreath, May 23, 2020.

  1. plaguebreath

    plaguebreath

    Joined:
    Aug 16, 2017
    Posts:
    16
    Hi everyone, I have a problem about rotation of my object (airplane) in a 3d space. I am moving it by applying force on his rigidbody and add torque for rotate. I put constrain on Y and Z rotation and X position I allow movement of my airplane only on Y and Z axis (I put camera on sideway for replicate a 2.5D game viewport) Now my problems are two actually, when I set my airplane with 0 value on all the rotations and let it go up and cut engine power suddenly the value of rotation of my airplane is (-90, 0,0) After a while for the gravity effect the airplane stop and rigidbody velocity point down, suddenly my rotation following the rigidbody velocity change in something like (91, -16, 143) making the airplane turn on the Y and Z axis and the same happen if I change the rotation on Y axis like -180 or if I make spin the airplane on the Z axis of 180 too.
    If my Y and Z starting rotation value are 0 I seems to had fix it by checking for difference between quaternion angles but not think it's the correct solution because it not work for Y and Z values other then 0, the code is something like that:

    Code (CSharp):
    1.  
    2. Vector3 dir = rigid.velocity;
    3.                 Quaternion lookq = Quaternion.LookRotation(dir, transform.up);
    4.                 if (Quaternion.Angle(transform.localRotation, lookq) >= 180f)
    5.                 {
    6.                     Quaternion ndir = Quaternion.FromToRotation(transform.position, dir);
    7.  
    8.                     Quaternion updatedRotation = Quaternion.Slerp(transform.rotation, ndir, Time.fixedDeltaTime * 0.8f);
    9.  
    10.                     rigid.MoveRotation(updatedRotation);
    11.  
    12.                 }
    13.                 else
    14.                 {
    15.                     Quaternion updatedRotation = Quaternion.Slerp(transform.rotation, lookq, Time.fixedDeltaTime * 0.5f);
    16.  
    17.                     rigid.MoveRotation(updatedRotation);
    18.                 }
    and below are the example pic I put to show the two case of rotations:

    I add an ugly image for explain my problem too because I guess my explanation not really clear :) Thank you for any help and best regards problems.jpg
     
  2. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    You could use something like Transform.LookAt to look at the position the plane is flying to (transform.position + rigidbody.velocity) and then set the Y and Z rotation to 0 doing something like this:

    Code (CSharp):
    1. Vector3 newRotation = transform.localEulerAngles;
    2. newRotation = new Vector3(newRotation.x, 0f, 0f);
    3.  
    4. transform.localRotation = Quaternion.Euler(newRotation);
     
  3. plaguebreath

    plaguebreath

    Joined:
    Aug 16, 2017
    Posts:
    16
    Hello
    I not understand this code, actually it does nothing, btw I am using on the rigidbody moverotation too not changing the transform rotation.
     
  4. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    Did you do the Transform.LookAt first before putting that code snippet? By itself it will do absolutely nothing because the rotation isn't being changed in that snippet.

    And sorry I missed that you were using the rigidbody.MoveRotation. If you're using that then just cache the initial rotation for the transform and reset it after getting the newRotation variable and set the rigidbody.MoveRotation to newRotation instead of setting the Transform.localRotation in the last step.
     
  5. plaguebreath

    plaguebreath

    Joined:
    Aug 16, 2017
    Posts:
    16
    I don't need to use Transform.LookAt here because I not need to turn my aiplane toward a world point, I need to make gradual change on the rotation toward a direction (rigidbody velocity)
     
  6. leftshoe18

    leftshoe18

    Joined:
    Jul 29, 2017
    Posts:
    61
    I was just suggesting it as a way to get a target rotation and then resetting it back to where it was like this:

    Code (CSharp):
    1. Quaternion cahcedRotation = transform.localRotation;
    2.  
    3. transform.LookAt(transform.position + rigidbody.velocity);
    4. Vector3 newRotation = transform.localEulerAngles;
    5. transform.localRotation = cahcedRotation;
    6.  
    7. newRotation = new Vector3(newRotation.x, 0f, 0f);
    8. Quaternion targetRotation = Quaternion.Euler(newRotation);
    9.  
    Then you can plug the targetRotation into your code. This should eliminate the rotation on the Y and Z axis like you wanted and will have no visible effect on the transform's rotation since it's reset after pulling the rotation.
     
  7. plaguebreath

    plaguebreath

    Joined:
    Aug 16, 2017
    Posts:
    16
    I understand you try to help so thank you but I did try the code and it just keep the plane straight pointed to forward direction.