- Home /
Question by
Simple-Beam17 · Mar 06 at 08:07 PM ·
rotationplayercamera rotaterotation axisrotating
How to "clamp" a y rotation in unity?
Hello! I am currently creating a game in Unity. In it, I have a player and a camera, controlled by the mouse, that freely rotates around the player, independent of the player's rotation. I want to prevent the camera from looking behind the player. But I'm not sure how to Allow all orientations of the camera except the y rotation of the cam that is greater than the player rotation +160 and less than the player rotation + 200. My camera orbit code is:
using UnityEngine;
using System.Collections;
public class Orbit : MonoBehaviour {
public float turnSpeed = 4.0f;
public Transform player; // The player's transform
public float offsetY; // This offsets my cameras position from the player on the y
public float offsetZ; // This one is for the Z
public float lookAtOff; // This is set to 2, causing the camera to look slightly above the player
private Vector3 offset;
void Start () {
offset = new Vector3(player.position.x, player.position.y + offsetY, player.position.z + offsetZ); // Set camera's offset position
}
void LateUpdate()
{
offset = Quaternion.AngleAxis (Input.GetAxis("Mouse X") * turnSpeed, Vector3.up) * offset; // Rotate around player based on Mouse X around the y axis
transform.position = player.position + offset; // Set camera position
transform.LookAt(player.position + new Vector3(0f, lookAtOff, 0f)); // Look slightly above the player
}
}
Any help would be appreciated. Thanks!
Comment