Question by
tobyallwood123 · Mar 06, 2019 at 09:57 AM ·
rotationpivotconstraints3rd person camera
Stopping 3rd person camera from looking too high or low?
I've managed to set up the simple rotation around the player of the third person camera (it uses an xbox controller) but I can't figure out how to stop the camera from going over 30 degrees and -30 degrees vertically. I've tried different versions of this code too and it always comes out really buggy. This script is attached to a pivot which follows the player, and the camera is attached to the pivot.
public class CameraMove : MonoBehaviour
{
public GameObject cam;
public GameObject player;
public float inputV;
public float inputH;
public float maxAngle = 30.0f;
public float minAngle = -30.0f;
// Update is called once per frame
void Update()
{
transform.position = new Vector3(player.transform.position.x, player.transform.position.y + 1, player.transform.position.z);
cam.transform.LookAt(transform);
float speed = 5.0f;
inputV = Input.GetAxisRaw("Joystick2V");
inputH = Input.GetAxisRaw("Joystick2H");
if (inputV != 0)
{
if (transform.eulerAngles.x > minAngle && transform.eulerAngles.x < maxAngle)
{
transform.Rotate(Vector3.left, inputV * speed);
}
else
{
if (transform.eulerAngles.x < (minAngle + 0.1f))
{
if (inputV < 0)
{
transform.Rotate(Vector3.left, inputV * speed);
}
}
if (transform.eulerAngles.x > (maxAngle - 0.1f))
{
if (inputV > 0)
{
transform.Rotate(Vector3.left, inputV * speed);
}
}
}
}
transform.Rotate(Vector3.up, inputH * speed);
transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, 0.0f);
}
}
Comment
Answer by AaronBacon · Mar 06, 2019 at 11:57 AM
Pretty sure you can just use a FixedUpdate check that checks if transform.Rotate =< 30 and if so, check if the players Vertical input is trying to move the camera up further, and block it if they are.