#Rigibody Player Jumping Stuck!

1 messages · Page 1 of 1 (latest)

tulip wolf
near void
#

Can we see some code?

tulip wolf
# near void Can we see some code?

Player Motor script :

using System.Runtime.InteropServices.WindowsRuntime;
using UnityEditor.Callbacks;
using UnityEngine;


[RequireComponent(typeof(Rigidbody))]
public class PlayerMotor : MonoBehaviour
{
    private Rigidbody rb;
    [SerializeField] float moveSpeed;
    [SerializeField] float jumpForce;
    [SerializeField] LayerMask groundLayer;
    bool isSprinting = false;
    Transform playerCamera;  // Reference to the camera

    private void Awake()
    {
        rb = GetComponent<Rigidbody>();
        playerCamera = Camera.main.transform;  // Assign the camera transform
    }

    public void Move(Vector2 input)
    {
        
        // Get the forward direction of the camera
      
        float horizontal = input.x;
        float vertical = input.y;

        Vector3 localMoveDirection = new Vector3(horizontal,0,vertical);
        Vector3 moveDirection = transform.TransformDirection(localMoveDirection)*moveSpeed;

        rb.velocity=new Vector3(moveDirection.x,0,moveDirection.z)*Time.fixedDeltaTime;
        
        

    }

    public void Jump()
    {
        if (IsGrounded())
        {
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }
    }

    public void Sprint()
    {
        if (!IsGrounded()) return;
        isSprinting = !isSprinting;
        moveSpeed = isSprinting ? moveSpeed*1.3f : moveSpeed/1.0f;
    }

    private bool IsGrounded()
    {
        RaycastHit hit;
        float rayLength = 1.5f;

        if (Physics.Raycast(transform.position, Vector3.down, out hit, rayLength, groundLayer))
        {
            return true;
        }

        return false;
    }
}
near void
#

Just to start, where and how do you call the Jump() ? Cause I don't see any Update() here. Also, are you using the new input system or the old one?

tulip wolf
#

this is the Input manager :

    using TMPro;
    using Unity.VisualScripting;
    using UnityEngine;
    using UnityEngine.InputSystem;

    [RequireComponent(typeof(PlayerMotor))]
    public class InputManager : MonoBehaviour
    {
        private PlayerActions playerActions;
        private PlayerMotor playerMotor;
        private CameraLook cameraLook;

        private void Awake()
        {
            playerActions = new PlayerActions();
            playerMotor = GetComponent<PlayerMotor>();
            cameraLook = GetComponentInChildren<CameraLook>();
            playerActions.OnFoot.Jump.performed += ctx => playerMotor.Jump();
            playerActions.OnFoot.Sprint.started += ctx => playerMotor.Sprint();
            playerActions.OnFoot.Sprint.canceled += ctx => playerMotor.Sprint();
        }

        private void OnEnable()
        {
            playerActions.OnFoot.Enable();

        }

        private void OnDisable()
        {
            playerActions.OnFoot.Disable();
        }

        private void FixedUpdate()
        {
            playerMotor.Move(playerActions.OnFoot.Movement.ReadValue<Vector2>());

        }

        private void LateUpdate()
        {
            cameraLook.Look(playerActions.OnFoot.Look.ReadValue<Vector2>());
        }
    }


near void
#

Have you tried debuging that isGounded?

tulip wolf
#

how ?

near void
#

First you can try and draw that raycast so you can see it properly

tulip wolf
#

yeah i did

#

it works without problems

near void
#

Second you can use some Debug.Log so you can check the bool's value

#

It might be a problem where the bool gets true while the player is in the air

tulip wolf
#

isGrounded is working without problems , i think the problem is from rigibody , look at this :

near void
#

Indeed... So I think the problem isn't with the rigidbody itself but with the way you're moving the player.
When you set the y-component of the rb.velocity to 0, you are effectively nullifying any influence of gravity or other forces acting on the y-axis each time you move. It's generally not a good practice to directly set rb.velocity because it can interfere with physics simulation. If you need to change the horizontal movement only, you should modify the x and z components of the velocity but leave the y component as it is

tulip wolf
#

this is the result :

near void
#

What if you would try something like : rb.velocity = new Vector3(moveDirection.x, rb.velocity.y, moveDirection.z);

tulip wolf
near void
#

And? It works the same?

tulip wolf
#

the same problem with it

#

but look I've just discovered something.

near void
#

What if you would try with a SphereCheck?

#

And make the radius really small like 0.4 or so

tulip wolf
#

no , not working

#

the isGrounded not working with this radius

#

not event with 1