I’ve been muddling my way through unity the hard way, that is, coding everything from the ground up. I’ve looked at others code sparingly and only to get a sense of why something is done the way it is. However, I’m having trouble limiting my camera’s vertical look. I have the players move directions down, and the rotation parallel to the plane, but I’m hitting a snag in limiting the player’s view up and down.
public class Player : MonoBehaviour {
public float speedCoefficient;
public float reverseCoef;
public GameObject camera;
// Use this for initialization
void Start () {
}
void Awake() {
}
// Update is called once per frame
void Update () {
float movementAmountStrafe = Input.GetAxis("Horizontal") * speedCoefficient * Time.deltaTime;
float movementAmountForward = Input.GetAxis("Vertical") * speedCoefficient * Time.deltaTime;
float movementLookY = Input.GetAxis ("Mouse X");
float movementLookX = Input.GetAxis ("Mouse Y");
transform.Rotate(0,movementLookY,0);
//Debug.Log (camera.transform.eulerAngles.x);
if (camera.transform.localEulerAngles.x<70 | camera.transform.localEulerAngles.x>290) {
camera.transform.Rotate(movementLookX*-1,0,0);
}
else {
Debug.Log ("No Cam");
if (camera.transform.localEulerAngles.x>70 camera.transform.localEulerAngles.x<75){
camera.transform.rotation = Quaternion.Euler(69.99f,0,0);
}
else if (camera.transform.localEulerAngles.x<290 camera.transform.localEulerAngles.x>285){
camera.transform.rotation = Quaternion.Euler(290.99f,0,0);
}
}
transform.Translate(movementAmountStrafe,0,0);
if (movementAmountForward<0) {
transform.Translate(0,0,movementAmountForward*reverseCoef);
}
else {
transform.Translate(0,0,movementAmountForward);
}
}
}
The system catches when the player tried to look outside of their ranges and keeps the border pretty smooth. However, if the character is facing the opposite direction from their original location, when you hit the border in the vertical, both the camera and player are flipped back towards the original direction. Perhaps how I’ve implemented this isn’t the right way, but logically it does what it’s supposed to. Any help would be appreciated. (There’s no real commenting because of how explicitly named the variables are.)