Can't Jump In Unity 3D Platformer Game

I am currently working on a 3D platformer game where I want to add a jump mechanic to it. I wrote the code like this

using UnityEngine;

public class Jumping : MonoBehaviour
{
public float jumpForce = 5f;
public LayerMask Ground;

private Rigidbody cubeRigidbody;

void Start()
{
    cubeRigidbody = GetComponent<Rigidbody>();
}

void Update()
{
    isGrounded = Physics.Raycast(transform.position, Vector3.down, 0.1f, Ground);

    // Check for player input to trigger a jump using the spacebar
    if (isGrounded && Input.GetKey(KeyCode.Space))
    {
        cubeRigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    }
}

private bool isGrounded;

private void OnDrawGizmosSelected()
{
    Gizmos.color = Color.red;
    Gizmos.DrawRay(transform.position, Vector3.down * 0.1f);
}

}

I placed that C# scripting language code onto the player and the ground platform objects. But when I clicked play, nothing happened. Space, up arrow and nothing. I tried using the same code for another project and it still doesn’t work. Is there an error in my code? Or did I just not implement it properly in my Unity project?

My player object has a box collider, 3D rigidbody and my ground object has a box collider.
My unity version is 2020.3.0f1

You are probably casting a ray that is too short. if your collider is larger than 0.1f then the ray will not hit anything. You could make the ray longer or add a offset to the origin to make it start from the bottom of your collider.

–

1 Answer

1

Edit: So ı just noticed the part above the code snippet.

Since there is a layermask ı am gonna assume that you have added a ground layer in inspector. Possible scenarios.

I believe your force value is too low. Use 100s.

Also move the code to the Fixed Update since it is better for physics.

I have increased the force value to 100 and to 200 as well but still nothing happened. When I checked the console for any notification of the player jumping, there was nothing at all. Yes, I have a ground platform object where the player is standing on. I placed the jumping scripting language on both the player and the ground platform. I don't know if that makes a difference but if it does, then should I take out the jumping scripting language component out of the player? Also for my input manager I already placed in space for the jumping section.

–

Your ground object doesnt need the jumping code. It only need to be on the "Ground" layer, which you can set in the inspector Under its name there are "tag" and "layer" dropdowns. use layer dropdown to add the ground layer. After that your player object(one with the jumping script) should have a dropdow called "Ground" Select "Ground" layer from that dropdown. Also the distance between the pivot of your object and the ground is important. Try increasing the raycast distance if you have already set the ground layer in both objects. I am assuming this is either a layer issue or a raycast issue.

–

I have tried the code with everything i just said. Probably distance between your player objects pivot and the plane are too far away(more than 0.1f which you use). I have added a Ground layer, set the ground objects layer as ground. then in Player object ı have set the layer mask as "Ground" as well. I used a default cube for player so the distance to ground object was more than .1f so i set that as .6f since a cube is 1 meter on each side and the pivot is in the middle .51f would work too if you need a bit more precision. Do try it and let me know.

–

I got the jumping mechanic to work! I changed the raycast value to 0.3f isGrounded = Physics.Raycast(transform.position, Vector3.down, 0.3f, Ground);. But I can only jump once where I cannot jump a second time in the game. I think it is because I did not add a slippery physics component to the ground object where the raycast needs to detect the player flat on the ground instead of rolling, but I want my player to roll and jump at the same time. Is it possible to make the player roll and jump and have the raycast detect it?

–

I can make the character jump multiple times(its a cube though). It might be that after jumping your raycast is not hitting the ground. Add; Debug.DrawRay(transform.position,Vector3.down*.3f,Color.red); this bit of code after your raycast, also turn your 0.3f to a public float variable and use that in raycast and debug for now so that you can test the distance while playing. You can see the ray you are shoting with the debug code in the 'Scene' tab.

–