- Home /
The question is answered, right answer was accepted
mouse look not working in first person controller script
Hello! I want to make a first person controller for my project, so I searched some tutorials and found a sample of code. However when I try and run it, it outputs a UnassignedReferenceException
error, saying that the playerCamera
variable has not been assigned. I already went through the code and I don't know how to assign it, and if I don't, the mouse look won't work and the player won't be able to look around. here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class SC_FPSController : MonoBehaviour
{
public float walkingSpeed = 7.5f;
public float runningSpeed = 11.5f;
public float jumpSpeed = 8.0f;
public float gravity = 20.0f;
public Camera playerCamera;
public float lookSpeed = 2.0f;
public float lookXLimit = 45.0f;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
float rotationX = 0;
[HideInInspector]
public bool canMove = true;
void Start()
{
characterController = GetComponent<CharacterController>();
// Lock cursor
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
// player is grounded, so recalculate move direction based on axes
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
// Press Left Shift to run
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runningSpeed : walkingSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpSpeed;
}
else
{
moveDirection.y = movementDirectionY;
}
// applies gravity
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
// move the controller
characterController.Move(moveDirection * Time.deltaTime);
// player and camera rotation
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
I don't have experience using Unity and this is literally my first project. Any help is very much welcome! thank you.
Answer by johnnikson2012 · Feb 18, 2021 at 06:10 PM
in your script line 13 you declare player camera you need to manually attach it just click on object where script has been attached and see the right side components where player cemare will be none just drag and simply drop in there that's all
Follow this Question
Related Questions
Why wont my camera controller work,Why isnt my script working for a camera controller? 1 Answer
How to control mouse look of standard First Person Controller? [Unity 5.x] 1 Answer
Override MouseLook/FirstPersonController 0 Answers
FirstPersonController script on Network 0 Answers
How do I make a crosshair? 2 Answers