Interpolate rotation between two objects

Hello, Im having troubles trying to make my player look at another object. Player is a flying vehicle so it rotates on axis X and Y, with the following code

            if ((leftVerticalInput == 1 && rotX< 50) || (leftVerticalInput == -1 && rotX> -50))
            {
                leftVerticalRotationSpeed = 40;

                rotX+= leftVerticalInput * Time.deltaTime * leftVerticalRotationSpeed;
            }
            else
            {
                leftVerticalRotationSpeed = 0;
            }


            if (leftVerticalInput == 0 && rotX> 2.5f)
            {
                leftVerticalRotationSpeed = 30;

                leftVerticalInput = -0.5f;
                rotX+= leftVerticalInput * Time.deltaTime * leftVerticalRotationSpeed;
            }
            else if (leftVerticalInput == 0 && rotX< -2.5f)
            {
                leftVerticalRotationSpeed = 30;

                leftVerticalInput = 0.5f;
                rotX+= leftVerticalInput * Time.deltaTime * leftVerticalRotationSpeed;
            }

       
            transform.Rotate(0, leftHorizontalInput * Time.deltaTime * leftHorizontalRotationSpeed, 0, Space.World);



            transform.Rotate(leftVerticalInput * Time.deltaTime * leftVerticalRotationSpeed, 0, 0, Space.Self);

As you can see, Im saving the X axis rotation on a variable in order to clamp this axis rotation. But in certain cases (facing a treasure or an enemy), I need to make player look at some object (smoothly) and later return the rotation control to the player. The problem is that using transform.rotate and a lerp function, or look at, or anything I can think of, make me loose the control of rotX variable, so when the control returns to the player, this variable is outdated and no longer working properly. Any idea? Thanks.

What is the rotX variable for exactly? It seems like you might be able to instead calculate the offset of the local forward vector to the player’s forward vector and use that instead. This way you wouldn’t have to store the change in rotation and could switch control of the player’s view around more easily.

1 Like

I use rotX to keep track of player rotation on X axis. When it reaches 50 or -50 I can stop player rotation on that axis. I tried using vector and quaternion calculations between original and current transform.forward, in order to get the difference angle and stop player if reaches 50/-50. And it works fine if I only rotate on X axis, but the problem is when player rotates Y axis too. Then the angle difference on X axis is not the same.