Object immediatly Changes rotation after pressing play...

Hi… I’ve got this script

    void FixedUpdate()
    {

        Vector3 mousePositionVector3 = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);

        mousePositionVector3 = Camera.main.ScreenToWorldPoint (mousePositionVector3);

        Vector3 targetdir = mousePositionVector3 - transform.position;
        transform.rotation = Quaternion.LookRotation (Vector3.up, targetdir);


        MovementUpDown ();
        MovementForward ();
//        Rotation ();
        ClampingSpeedValues ();
        Swerve ();

        DroneRB.AddRelativeForce (Vector3.up * upForce);
        DroneRB.rotation = Quaternion.Euler (
            new Vector3 (tiltAmountForward, 0, tiltAmountSideways)
        );
    }

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!

also the Input.mousePosition.x is not the problem as I’ve changed it to 0 as well gives same result

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.

    void FixedUpdate()
    {
    
        rotationY = Mathf.SmoothDamp (transform.rotation.eulerAngles.y, Cam.transform.rotation.eulerAngles.y, ref rotationYvelocity, 0.5f);


//        Vector3 mousePositionVector3 = new Vector3 (0, Input.mousePosition.y, 0);
//        mousePositionVector3 = Camera.main.ScreenToWorldPoint (mousePositionVector3);
//        Vector3 targetdir = mousePositionVector3 - transform.position;
//        transform.rotation = Quaternion.LookRotation (Vector3.up, targetdir);
//

        MovementUpDown ();
        MovementForward ();
//        Rotation ();
        ClampingSpeedValues ();
        Swerve ();


        DroneRB.AddRelativeForce (Vector3.up * upForce);
        DroneRB.rotation = Quaternion.Euler (
            new Vector3 (tiltAmountForward, rotationY, tiltAmountSideways)
        );
    }

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

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 :confused:

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.

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!

Quaternion r1 = Quaternion.Euler(foo, 0, 0);
Quaternion r2 = Quaternion.Euler(0, bar, 0);
Quaternion r3 = Quaternion.Euler(0, 0, baz);
transform.rotation = r2 * r1 * r3;  // or whatever order you prefer