Question by
got2axes · Jan 24, 2018 at 09:09 PM ·
cameracamera-movementcamera-lookcamera rotationthird-person-camera
,how to set max rotation for camera in third person game
i have this script wich allows me to look upand down with third person camera but the prob is when i move the mouse down or up it goes either under the ground or so far up so i was wondering if there was any way to limit the vertical rotation
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraMouseLook : MonoBehaviour {
public GameObject target;
public float rotateSpeed = 5;
Vector3 offset;
void Start() {
Cursor.visible = false;
offset = target.transform.position - transform.position;
}
void LateUpdate() {
float horizontal = Input.GetAxis ("Mouse X") * rotateSpeed;
float vertical = Input.GetAxis ("Mouse Y") * rotateSpeed;
target.transform.Rotate (vertical, horizontal, 0);
float desiredAngleH = target.transform.eulerAngles.y;
float desiredAngleV = target.transform.eulerAngles.x;
Quaternion rotation = Quaternion.Euler (desiredAngleV, desiredAngleH, 0);
transform.position = target.transform.position - (rotation * offset);
transform.LookAt (target.transform);
}
}
Comment