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.