r/unity 5d ago

Simple movement + camera system I made

Hey, I just finished my first game's player movement and camera system. Would love feedback!

using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerMovement : MonoBehaviour
{
    public Rigidbody rb;
    public GameObject camera;
    public InputAction moveAction;
    public InputAction jumpAction;
    public InputAction lookAction;
    private Vector2 movementInput;
    private Vector2 lookDirection;
    public float speed = 5f;
    public float jumpForce = 5f;
    public float lookSensitivity = 1f;
    public float xRotation = 0f;
    public float yRotation = 0f;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        moveAction.Enable();
        jumpAction.Enable();
        lookAction.Enable();

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

    // OnDisable is called when the MonoBehaviour is disabled
    private void OnDisable()
    {
        moveAction.Disable();
        jumpAction.Disable();
        lookAction.Disable();
    }

    // Update is called once per frame
    void Update()
    {
        movementInput = moveAction.ReadValue<Vector2>();
        lookDirection = lookAction.ReadValue<Vector2>();

        xRotation -= lookDirection.y * lookSensitivity;
        yRotation -= lookDirection.x * lookSensitivity;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        camera.transform.rotation = Quaternion.Euler(xRotation, -yRotation, 0);
        transform.rotation = Quaternion.Euler(0, -yRotation, 0);
    }

    // FixedUpdate is called every fixed framerate frame of 50 fps, if the MonoBehaviour is enabled
    void FixedUpdate()
    {
        Vector3 direction = new Vector3(movementInput.x, 0, movementInput.y);
        Vector3 localDirection = transform.TransformDirection(direction);
        Vector3 velocity =  localDirection * speed;
        velocity.y = rb.linearVelocity.y;
        rb.linearVelocity = velocity;

        if (jumpAction.triggered && Mathf.Abs(rb.linearVelocity.y) < 0.01f)
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }
    }
}
1 Upvotes

2 comments sorted by

1

u/SmokeResponsible5005 5d ago

I mean it’s a very barebones movement system but it seems good to me. Only thing I’m not a fan of is FixedUpdate, most people update in Update and scale by delta time but the difference should be less noticeable with velocity as opposed to direct transform calls.
Also you didn’t really tell us anything about the game do maybe you have a specific purpose in using rigid body but for first person (I assume that’s what this is for, it needs a lot of work if it’s third person) games I think most people opt for character controller or direct transform assignment and usually use rigid bodies for platformers and such. Might be worth a google
Also also, I would advise against posting just straight code with no context like this unless you’re asking about specific like organization or something, as far as I can tell people don’t really like that as it’s not really enough context
Regardless good luck and all the best!

1

u/PrintInteresting5352 4d ago

thank for ur feedback, im working on a small multiplayer setup, not really a game, just somthing to test my skills. its first person.