using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
public float sensitivity = 20f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (this.transform.rotation.eulerAngles.x <= 45 && this.transform.rotation.eulerAngles.x >= 0)
{
transform.Rotate(-Input.GetAxis("Mouse Y") * Time.deltaTime * sensitivity, 0f, 0f, Space.Self);
}else if (this.transform.rotation.eulerAngles.x >= 45)
{
if(Input.GetAxis("Mouse Y") > 0)
{
transform.Rotate(-Input.GetAxis("Mouse Y") * Time.deltaTime * sensitivity, 0f, 0f, Space.Self);
}
}else if (this.transform.rotation.eulerAngles.x <= 0)
{
Debug.Log("Test");
if (Input.GetAxis("Mouse Y") < 0)
{
transform.Rotate(-Input.GetAxis("Mouse Y") * Time.deltaTime * sensitivity, 0f, 0f, Space.Self);
}
}
}
}
I am trying to make a code that rotates the camera on x axis with mouse movement, but it works only with the down limit. The up limit doesn’t work. Please help
always work towards maintaining your code. The following format is usually easier to read, so try to use it in your code style, avoid using ‘else’ where posible. At later stages you will have to debug it to find nasty errors, - your future-self will be thankful if comments are written and there are few indentation levels (as less of nested if’s as possible).
using UnityEngine;
using System.Collections;
public class MouseLook : MonoBehaviour {
public float sensitivity = 20f;
void Update() {
if (Input.GetAxis("Mouse Y") < 0) {
Do_negative_x_rotate();
}
if (Input.GetAxis("Mouse Y") > 0) {
Do_positive_x_rotate();
}
}//end Update()
void Do_negative_x_rotate() {
if (this.transform.rotation.eulerAngles.x <= 0) {
Debug.Log("Test");
transform.Rotate( -Input.GetAxis("Mouse Y") * Time.deltaTime * sensitivity,
0f,
0f,
Space.Self);
return;
}
}//end ()
void Do_positive_x_rotate() {
// notice, 0 first, 45 after (in growing order)
if (this.transform.rotation.eulerAngles.x >= 0
&& this.transform.rotation.eulerAngles.x <= 45) {
transform.Rotate( -Input.GetAxis("Mouse Y") * Time.deltaTime * sensitivity,
0f,
0f,
Space.Self);
}
// notice, GREATER (not equals) to 45, it was equals already 'less or EQUALS 45'
// in the check above.
if (this.transform.rotation.eulerAngles.x > 45) {
transform.Rotate( -Input.GetAxis("Mouse Y") * Time.deltaTime * sensitivity,
0f,
0f,
Space.Self);
}
}//end ()
}