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);
}
}