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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Object immediatly Changes rotation after pressing play...

Discussion in 'Getting Started' started by ShinigamiUzi, Jan 26, 2018.

  1. ShinigamiUzi

    ShinigamiUzi

    Joined:
    Sep 5, 2017
    Posts:
    54
    Hi.. I've got this script

    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.  
    4.         Vector3 mousePositionVector3 = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
    5.  
    6.         mousePositionVector3 = Camera.main.ScreenToWorldPoint (mousePositionVector3);
    7.  
    8.         Vector3 targetdir = mousePositionVector3 - transform.position;
    9.         transform.rotation = Quaternion.LookRotation (Vector3.up, targetdir);
    10.  
    11.  
    12.         MovementUpDown ();
    13.         MovementForward ();
    14. //        Rotation ();
    15.         ClampingSpeedValues ();
    16.         Swerve ();
    17.  
    18.         DroneRB.AddRelativeForce (Vector3.up * upForce);
    19.         DroneRB.rotation = Quaternion.Euler (
    20.             new Vector3 (tiltAmountForward, 0, tiltAmountSideways)
    21.         );
    22.     }
    what the first 4 lines do is to get the position of the mouse and rotates the object only on the Y AXIS (vector3.up) to the position of the mouse..

    on the last line I've added the quaternion.euler of the X and Z which I calculated separately..and left Y at 0 so the rotation of that will be by mouse.. here's annoying part:

    After I've added the first 4 lines and changed the Drone.RB Y rotation's to 0, then hitting Play the object rotates with mouse but now the X Quaternion is rotated -90 for some reason, and nose of the object is looking up instead of forward... i'm asking if there's a better way to write this? Really appreciate any advice!
     
  2. ShinigamiUzi

    ShinigamiUzi

    Joined:
    Sep 5, 2017
    Posts:
    54
    also the Input.mousePosition.x is not the problem as I've changed it to 0 as well gives same result
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Yes, it's generally best not to mess with the individual euler angles like this (except when you're only using one — but you're using all three).

    Instead you should compose rotations by multiplying them together. R2 * R1, where these are quaternions, gives you a rotation by R1 followed by a rotation by R2.

    So, I don't entirely understand what you're trying to achieve, but I'm pretty sure the right solution is to create a one-axis rotation for each of your three inputs (yaw by mouse; pitch and roll by whatever determines those tilts), and then just multiply them together in the correct order.
     
  4. ShinigamiUzi

    ShinigamiUzi

    Joined:
    Sep 5, 2017
    Posts:
    54
    Code (CSharp):
    1.     void FixedUpdate()
    2.     {
    3.    
    4.         rotationY = Mathf.SmoothDamp (transform.rotation.eulerAngles.y, Cam.transform.rotation.eulerAngles.y, ref rotationYvelocity, 0.5f);
    5.  
    6.  
    7. //        Vector3 mousePositionVector3 = new Vector3 (0, Input.mousePosition.y, 0);
    8. //        mousePositionVector3 = Camera.main.ScreenToWorldPoint (mousePositionVector3);
    9. //        Vector3 targetdir = mousePositionVector3 - transform.position;
    10. //        transform.rotation = Quaternion.LookRotation (Vector3.up, targetdir);
    11. //
    12.  
    13.         MovementUpDown ();
    14.         MovementForward ();
    15. //        Rotation ();
    16.         ClampingSpeedValues ();
    17.         Swerve ();
    18.  
    19.  
    20.         DroneRB.AddRelativeForce (Vector3.up * upForce);
    21.         DroneRB.rotation = Quaternion.Euler (
    22.             new Vector3 (tiltAmountForward, rotationY, tiltAmountSideways)
    23.         );
    24.     }
    Something like this perhaps? on the smoothdamp unless I put 0f it will create this weird affect when rotating fast where the object changes forward position to back position
     
  5. ShinigamiUzi

    ShinigamiUzi

    Joined:
    Sep 5, 2017
    Posts:
    54
    here's how it looks: first when smoothdamp has a time value of 0



    and when smoothdamp has a value of 0.1 or above, it does those weird spins..



    I would like for a way to smooth the rotation without those spins happening ... not sure how to do it :/
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    No, nothing like that. I was suggesting you set up three separate one-axis rotations, and multiply them together. You're doing a single 3-axis rotation all at once.

    However, if you like the order in which Quaternion.Euler does it, then that's fine.

    The problem may be that you're using Mathf.SmoothDamp with angles. That function doesn't understand that the numbers you give it represent angles, and so it won't (for example) interpolate smoothly from 359 to 0 as a 1-degree change; it'll go the long way, from 359 through 358, 357, etc., all the way down to 0. To work with angles properly, you should use Mathf.SmoothDampAngle instead.
     
  7. ShinigamiUzi

    ShinigamiUzi

    Joined:
    Sep 5, 2017
    Posts:
    54
    Can you send me example of the 3 one axis rotation multiply method ? perhaps a unity documentations link?

    and I will see how it looks with SmoothDampangles, thanks!

    EDIT: Yes, SmoothDampAngle is definitely an improvement! thanks.. I still want to learn about the other method tho if you can link me example!
     
    Last edited: Jan 27, 2018
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    Code (CSharp):
    1. Quaternion r1 = Quaternion.Euler(foo, 0, 0);
    2. Quaternion r2 = Quaternion.Euler(0, bar, 0);
    3. Quaternion r3 = Quaternion.Euler(0, 0, baz);
    4. transform.rotation = r2 * r1 * r3;  // or whatever order you prefer