First person jumping not working as expected - error partially identified

Hey,

I’m a beginner with Unity and I’m practicing with a first-person movement system after following some tutorials. I’ve already set it up so the camera follows the mouse, and I’ve managed to set the rotation and the get the player to move in all directions based on where the camera is pointing.

I’m now trying to implement jumping using the rigidbody, but whenever I press the button the player’s location immediately goes to the top of the jump, and then they slowly and shakily drift down. The jump was smooth before, acting as it should with the player going up and down evenly, but since adding the MovePlayer() function now the jump doesn’t work as expected.

I suspect it’s to do with the “rotatedMovement” vector being updated constantly, but I’m not sure if this is correct, or how to resolve the issue.

Here’s what I’ve got so far:

using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.InputSystem; 

public class PlayerController : MonoBehaviour
{
    private PlayerControls playerControls;
    private Rigidbody rb;

    public float jumpForce = 20.0f;
    public float moveSpeed = 5.0f;

    private bool isGrounded = true;

    private Vector3 playerMovement;
    private Vector3 rotatedMovement;

    void Start()
    {
        playerControls = new PlayerControls();
        rb = GetComponent<Rigidbody>();
    }

    private void OnMove(InputValue inputValue)
    {
        playerMovement = inputValue.Get<Vector2>();
    }

    private void OnJump()
    {
        if (isGrounded)
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
            isGrounded = false;
        }
    }

    private void MovePlayer()
    {
        rotatedMovement = transform.rotation * new Vector3(playerMovement.x, 0, playerMovement.y);
        rb.linearVelocity = rotatedMovement * moveSpeed;
    }

    void Update()
    {
        MovePlayer();
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

Any guidance would be greatly appreciated :slight_smile:

Thanks

Writing FPS controllers is pretty advanced stuff. Here’s some reference materials, two existing fully-featured ones you can use:

Here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

That one has run, walk, jump, slide, crouch.

And here is a free Kinematic Character Controller that also comes with a huge playground:

^This, for any major components that are usually used in projects just try and find free systems / packages, the entire point of using an engine is to speed up development, no point in trying to reinvent the wheel for something people have already put the work in and released for free.

To test this, add an if (!isGrounded) return; statement at the beginning of the MovePlayer method.

In general, if you think a specific piece of code causes problems in a certain state, prevent that code from running in that state. If your hypothesis is correct, expand your investigation from there.

Yeah like Kurt indicated, you got to ask yourself whether you want to learn how to re-invent the wheel (aka character controller) or whether you want to learn actual game programming/development.

Something tells me that the tutorial also has you script Camera.main.transform with movement and rotation, is that right? If so, ditch that. We have Cinemachine, setting up first/third person (or orbit, top-down, isometric, ..) camera motion is just a few clicks away and it behaves like in AAA games, including camera shakes and those sort of things.

Unfortunately, most of the tutorials out in the wild teach you exactly that: the things nobody needs to write code for anymore. Because the rest would actually mean the tutorial author spend some time coming up with ideas in the first place, let alone how to write that code. Even things like Inventory, Stats, Dialogues, and so forth you can either learn DIY from tutorials, or get an asset from the store or GitHub.

It’s really up to you what you wish to learn with. It’s okay to do the character controller, just know that the way tutorials write these is for quick-wins only, they don’t actually work like “real” character controllers quality-wise.