C# Mouse Look Script | Why does the Y Axis Work but not the X Axis?
Hello!
I've been teaching myself the basics of Unity lately by creating a first-person game.
Everything works except for my C# mouse look code. The camera rotates up and down on a clamped Y axis (as desired), however, the camera won't rotate left or right on the X-axis.
However, if I remove this code from the mouse look script:
mouseUpDown = Mathf.Clamp(mouseUpDown, -mouseLookLimit, mouseLookLimit); Camera.main.transform.localRotation = Quaternion.Euler(mouseUpDown, 0, 0);
The camera can rotate on the X-axis again.
I've posted my code below along with a screenshot of my inspector panel/character controller; can anyone explain what's gone wrong and how to fix it?
If you need anymore information or screenshots, let me know and I'll post it.
Thanks in advance!
Andy
C# Script:
using UnityEngine;
using System.Collections;
public class playerMovement : MonoBehaviour
{
public float moveSpeed, walkSpeed, runSpeed, crouchSpeed;
public float mouseSensitivity, mouseUpDown, mouseLeftRight, mouseLookLimit;
public float verticalJump, jumpStrength, jumpGravity;
CharacterController crouchMove;
CharacterController jumpMove;
void Start()
{
crouchMove = gameObject.GetComponent<CharacterController>();
jumpMove = gameObject.GetComponent<CharacterController>();
}
void Update()
{
//------------------- Forwards, Backwards, Strafe Side-to-Side Movement -------
float forwardBackMove = Input.GetAxis("Vertical") * moveSpeed;
float leftRightMove = Input.GetAxis("Horizontal") * moveSpeed;
Vector3 speed = new Vector3(leftRightMove, 0, forwardBackMove);
speed = transform.rotation * speed;
//------------------- Sprinting --------------------------------------------------------------------
if (Input.GetKey(KeyCode.LeftShift))
{
moveSpeed += 5;
if (moveSpeed >= runSpeed)
{
moveSpeed = runSpeed;
}
}
else
{
moveSpeed = walkSpeed;
}
//------------------- MOUSE LOOK SCRIPT -----------------------------------------------
mouseLeftRight = Input.GetAxis ("Mouse X") * mouseSensitivity;
transform.Rotate(0, mouseLeftRight, 0);
mouseUpDown -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
mouseUpDown = Mathf.Clamp(mouseUpDown, -mouseLookLimit, mouseLookLimit);
Camera.main.transform.localRotation = Quaternion.Euler(mouseUpDown, 0, 0);
//------------------- CROUCHING AND STANDING -----------------------------------
if (Input.GetKey(KeyCode.LeftControl))
{
crouchMove.height = 1.0f;
moveSpeed = crouchSpeed;
}
else
{
crouchMove.height += 0.3f;
if (crouchMove.height >= 1.96) {
crouchMove.height = 1.96f;
}
}
//------------------- JUMPING AND GRAVITY------------------------------------------------
if (jumpMove.isGrounded) {
verticalJump = -jumpGravity * Time.deltaTime;
if (Input.GetKey(KeyCode.Space)){
verticalJump = jumpStrength;
}
}
else
{
verticalJump -= jumpGravity * Time.deltaTime;
}
Vector3 jumpVector = new Vector3(0, verticalJump, 0);
jumpMove.Move(jumpVector * Time.deltaTime);
//------------------- CHARACTER CONTROLLER ----------------------------------------
CharacterController playerController = GetComponent<CharacterController>();
playerController.SimpleMove(speed);
}
}
Answer by metalted · Sep 06, 2016 at 09:25 PM
You have kind of figured it out yourself... almost!
mouseUpDown = Mathf.Clamp(mouseUpDown, -mouseLookLimit, mouseLookLimit); Camera.main.transform.localRotation = Quaternion.Euler(mouseUpDown, 0, 0);
You are setting the eulerAngles of the camera, no problem with that, but you are setting it to a Vector3(mouseUpDown, 0, 0); The eulerAngles of the camera in the y and z axis will always be zero; This may be the cause of the camera not rotating. Make sure that setting one rotation will not mess up the other rotation.
Your answer
Follow this Question
Related Questions
Clamping camera rotation 1 Answer
[C#]detect object on mouse look? 1 Answer
How To Make My Player Only Move on the ground? 1 Answer
How to Clamp a 3rd Person Camera's x-axis Rotation? 0 Answers