I’m having trouble with my camera, I an freely move it around using the Mouse Orbit script. I have a block of code that will snap the camera behind the player when I press Left Trigger. The problem is, once I let go of the button, the camera reverts back to the location it was previously, but I would like it to stay behind the player. Any help on this is appreciated
I have attached a photo to illustrate what happens, below is the code:
if (!playerIsTargeting)
{
Debug.Log("here");
x += Input.GetAxis(mouseX) * xSpeed;
y -= Input.GetAxis(mouseY) * ySpeed;
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
distance = Mathf.Clamp(distance - Input.GetAxis(mouseY) * speed, distanceMin, distanceMax);
height = Mathf.Clamp(height - Input.GetAxis(mouseY) * speed, heightMin, heightMax);
Vector3 negDistance = new Vector3(0, 1, -distance);
Vector3 position = rotation * negDistance + target.position;
transform.rotation = rotation;
transform.position = position;
}
else
{
distance = 7;
camPosition = target.position + transform.TransformDirection(new Vector3(0, 2, -distance));
playerRotation = Quaternion.LookRotation(target.transform.forward);
transform.position = Vector3.Slerp(transform.position, camPosition, 10.0f * Time.deltaTime);
transform.rotation = Quaternion.Slerp(transform.rotation, playerRotation, 10.0f * Time.deltaTime);
}
EDIT
So I’ve discovered after playing around why it snaps back to it’s original position. The camera is looking at the X and Y values, these do not change when I’m targeting so when I stop, the camera reverts back to these positions, so my question now is, is there a way of changing those X and Y values to the position behind the player?