r/Unity3D • • 1d ago

Question First person camera is not working

Hello! I followed the tutorial of someone named "Shark Games" how to do make first person movement in unity. I tried to follow step by step that tutorial including a fix that they suggested on the comments box; everything worked with the exception of my camera movement. Whenever I try to move my mouse to move the camera, nothing happens.

Link of the tutorial that I was following:

https://www.youtube.com/watch?v=v_ncMFEoHTg

Here is how my code looks like:

using UnityEngine;
using UnityEngine.InputSystem;


[RequireComponent(typeof(CharacterController))]


public class PlayerController : MonoBehaviour
{
    [SerializeField] private float WalkSpeed = 5.5f;
    [SerializeField] private float RunningSpeed = 9.0f;


    [SerializeField] private float JumpForce = 8.0f;
    [SerializeField] private float Gravity = 20.0f;


    [SerializeField] private float LookSensitivity = 0.2f;
    [SerializeField] private float LookAngleLimit = 90f;


    private Camera mainCamera;
    private CharacterController characterController;


    private InputAction moveInput;
    private InputAction runInput;
    private InputAction jumpInput;
    private bool jumped = false;


    private float currentMoveSpeed = 0.0f;
    private Vector3 moveDirection = Vector3.zero;
    private float lookAngle = 0.0f;


    void Start()
    {
        mainCamera = GetComponentInChildren<Camera>();
        characterController = GetComponent<CharacterController>();


        moveInput = InputSystem.actions.FindAction("Move");
        runInput = InputSystem.actions.FindAction("Sprint");


        jumpInput = InputSystem.actions.FindAction("Jump");
        jumpInput.started += Jumped;


        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;


        currentMoveSpeed = WalkSpeed;
    }


   
    void Update()
    {
        Vector2 moveVector = moveInput.ReadValue<Vector2>();
        Vector2 mouseDelta = Mouse.current.delta.ReadValue();


        if (!characterController.isGrounded)
            jumped = false;


        HandleMovement(moveVector);
    }


    private void HandleMovement(Vector2 moveVector)
    {
        Vector3 forward = transform.TransformDirection(Vector3.forward);
        Vector3 right = transform.TransformDirection(Vector3.right);


        float oldY = moveDirection.y;


        Vector2 newSpeed = new Vector2(moveVector.y * currentMoveSpeed, moveVector.x * currentMoveSpeed);


        moveDirection = (forward * newSpeed.x) + (right * newSpeed.y);
        moveDirection.y = (jumped && characterController.isGrounded) ? JumpForce : oldY;


        if (!characterController.isGrounded)
            moveDirection.y -= Gravity * Time.deltaTime;


        currentMoveSpeed = runInput.IsPressed() ? RunningSpeed : WalkSpeed;
        characterController.Move(moveDirection * Time.deltaTime);
    }


    private void Jumped(InputAction.CallbackContext _)
    {
        jumped = true;
    }


    private void HandleLooking(Vector2 mouseDelta)
    {
        lookAngle += -mouseDelta.y * LookSensitivity;
        lookAngle = Mathf.Clamp(lookAngle, -LookAngleLimit, LookAngleLimit);


        mainCamera.transform.localRotation = Quaternion.Euler(lookAngle, 0, 0);
        transform.rotation *= Quaternion.Euler(0, mouseDelta.x * LookSensitivity, 0);
    }
}
0 Upvotes

5 comments sorted by

3

u/Logical_Candidate523 1d ago

In the Update() function I see you read the mouse delta value but you never use it. You could send it to a HandleCamera(Vector2 mouseDelta) function and then rotate the camera with that. Probably you could also assign a variable for the camera sensitivity and multiply the mouseDelta with that and clamp the vertical rotation.

Edit: you actually have created that function already but you never call it in the Update() method. After handling the movement call the function HandleLooking(mouseDelta);

1

u/AdmirableOption3320 20h ago

I love you I love you I love you!!!! Thankssss!!!!

1

u/Logical_Candidate523 12h ago

You're welcome!