Character keeps floating when using Rigidbody based jump

Hello, i’m trying to make a 3d character controller.

Everything is okay, except for the jumping. The character jumps normally if the game is not reading any other input, but if i try to jump while moving, the character does not jumps almost at all, or if i move while jumping it keeps floating.

How to fix this?

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

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody playerRB;

    private float hInput;
    private float vInput;

    private Vector3 direction;

    [SerializeField] private float speed;
    [SerializeField] private float rotationSpeed;
    [SerializeField] private float jumpForce;

    [SerializeField] private Transform camDir;

    [SerializeField] private bool grounded;


    void Start()
    {
        playerRB = GetComponent<Rigidbody>();

    }

    
    void Update()
    {
        Movement();
        Jump();
    }

    private void FixedUpdate()
    {
       
    }

    void Movement()
    {
        //inputs
        hInput = Input.GetAxisRaw("Horizontal");
        vInput = Input.GetAxisRaw("Vertical");

        direction = new Vector3(hInput,0, vInput).normalized;
       
        //camera relations
        Vector3 camForward = camDir.transform.forward;
        Vector3 camRight = camDir.transform.right;

        camForward.y = 0;
        camRight.y = 0;

        Vector3 forwardRelative = camForward * vInput;
        Vector3 rightRelative = camRight * hInput;

        Vector3 relativeDirection = forwardRelative + rightRelative;




        if (direction.magnitude !=0)
        {          
            Quaternion lookDirection = Quaternion.LookRotation(relativeDirection, Vector3.up);
            //mueve al personaje
            playerRB.velocity = new Vector3(relativeDirection.x,0, relativeDirection.z) * speed * Time.deltaTime;
            //rota al personaje
            transform.rotation = Quaternion.RotateTowards(transform.rotation, lookDirection, rotationSpeed * Time.deltaTime);
        }
       
    }

    void Jump()
    {
        if(Input.GetButtonDown("Fire1") && grounded) { 
            playerRB.AddForce(playerRB.velocity.x, jumpForce, playerRB.velocity.z);
            grounded = false;       
        }
    }

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

It helps to post your code. It's hard to imagine what your code is.

1 Answer

1

“if i try to jump while moving, the character does not jumps almost at all”

It’s because when you’re moving, you’re setting the player’s y velocity to 0, which immediately stops the jumping force:

playerRB.velocity = new Vector3 (relativeDirection.x, 0, relativeDirection.z) * speed * Time.deltaTime;

To maintain the character jumping while moving in Movement(), get the RB velocity and only change the X and Z (movement) components:

var playerVelocity = playerRB.velocity;
playerVelocity.x = relativeDirection.x * speed * Time.deltaTime;
playerVelocity.z = relativeDirection.z * speed * Time.deltaTime;
playerRB.velocity = playerVelocity;

Also, make sure your rigidbody is set to use gravity.

worked, thanks!