- Home /
Limit Y axis rotation of camera
Good afternoon, I have a simple question but I couldn't find a solution in the forum that could suit to my code without completely modifying it. I added a Character Controller to my test capsule and I have the camera (myview) as the player's child. It's a first person player camera, the camera's view follows mouse direction, but I'd like to limit the up/down rotation so not to rotate vertically more than 90 degrees.
All the other topics I've found had a different code from mine and I'm not sure on how to proceed.
Here's my entire source code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterMovement : MonoBehaviour {
public float speed = 2.0f;
public float sensibility = 2.0f;
CharacterController player;
public GameObject myview;
float leftright;
float backforward;
float Xrotate;
float Yrotate;
// Use this for initialization
void Start () {
player = GetComponent<CharacterController>();
}
// Update is called once per frame
void Update () {
leftright = Input.GetAxis("Horizontal")*speed;
backforward = Input.GetAxis("Vertical")*speed;
Xrotate = Input.GetAxis("Mouse X")*sensibility;
Yrotate = Input.GetAxis("Mouse Y")*sensibility;
Vector3 movement = new Vector3(backforward, 0, leftright);
transform.Rotate(0, Xrotate, 0);
myview.transform.Rotate(-Yrotate, 0, 0);
movement = transform.rotation * movement;
player.Move (movement * Time.deltaTime);
}
}
Answer by Raimi · Jul 08, 2017 at 02:18 PM
transform.Rotate(Mathf.Clamp(Xrotate, -90, 90),0,0);
This is untested but Mathf.Clamp should clamp the value between a min and a max value. https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html
hope it helps
Your answer
Follow this Question
Related Questions
Help With Arrow Camera Script 2 Answers
camera RotateAround, limit angles. 0 Answers
How to Limit Z Axis on my FirstPersonCamera? 1 Answer
Limit Vertical (Y Position) of Player Camera on Joysticks? 0 Answers
Limit Y Axis transform.RotateAround 1 Answer