- Home /
Problem with camera position
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?
Answer by JayFitz91 · Aug 31, 2017 at 06:06 PM
Thanks for the response but I just managed to figure it out, the X and Y variables were not being changed while Left Trigger was held and thus, the camera position would snap back every time I let go of the Trigger.
So i changed the else clause in my code to keep the X value of the camera equal to the value as the players local Y position, maintaining the position of the camera when leaving the targeting transition
else
{
camPosition = target.position + transform.TransformDirection(new Vector3(0, 1, -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);
distance = 7;
x = target.localEulerAngles.y;
y = 0;
}
Answer by Duvic · Aug 31, 2017 at 06:00 PM
Yes, there are two easy ways
1) make your variables Public that way you can access them from another script.
2) make a Public function (void) where you are changing the variables in your script and call it from the trigger script.
Answer by ArturoSR · Aug 31, 2017 at 06:09 PM
@JayFitz91 Hello there, you only need to set an initial position variable or say it in other words: "Go behind the player", this variable must be private to avoid undesired changes on it, it could be something like this:
private Vector3 behindPlayer = new Vector3 (//The position you need);
private void Start () {
behindPlayer = camera.position; //This is if the camera it's already behind;
}
if (Input.GetKeyUp ("Aim")) {
camera.position = behindPlayer;
}
This is just an example, but it's something like that, obviously, you can make the transition more smoothly to avoid the "snap to view X" effect, hopes this helps, cheers.