#Basic First Player Camera/Movement Debug/Review

1 messages · Page 1 of 1 (latest)

umbral wren
#

Hello, just made my script "complex" to manage my camera on first person view. Associated with another Script for my player movement (based on mouse position).

I would like help to understand the problem in it and fix it to have a solid base for my project (learn unity and have fun with it).
I am afraid that in this state I would swim in bugs in 2days.

Thing that I suppose are strange:

  • My speed goes really fast to max, but is not constant.
  • I have to do a speed control, or it can go quite hight.
    -My player rigidebody has velocity when I move my camera.
    -I have to update my player rotation with camera rotation.
    -When I turn camera seing entity moving I get like a shaking effect.

I am really new with this Unity so I could be doing really obvious errors.

Thanks in advance.

#

PlayerCamera.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerCamera : MonoBehaviour
{
    public float sensX;
    public float sensY;

    public Transform playeryOrientation;

    float xRotation;
    float yRotation;

    // Start is called before the first frame update
    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }  

    // Update is called once per frame
    void Update()
    {
        // get mouse input
        float mouseX = Input.GetAxis("Mouse X") * sensX * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * sensY * Time.deltaTime;

        yRotation += mouseX;
        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        // rotate camera and orientation
        transform.rotation = Quaternion.Euler(xRotation, yRotation, 0);
        playeryOrientation.rotation = Quaternion.Euler(0, yRotation, 0);
    }
}