Player Not Jumping When on Terrain, Only Jumps on Flat Areas of Map

I have a player with Character Controller and a Player Movement Script (See Image).

My groundcheck is attached under Player which is the parent and is at the bottom of the player body. With no component.

When my player goes on terrain, 90% of my map, the player wont jump, pretty sure the ground check doesnt think its the ground. But as soon as i go to flat land it seems to work like wooden, concerte and etc.

Here is my Player movement script file and the same thing but copied pasted here, and the values if needed for the variables if changed are in image 1 at the top.

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

public class PlayerMovement : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;
    public float gravity = -9.81f;
    public float jumpHeight = 3f;

    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;

    Vector3 velocity; 
    bool isGrounded;

    // Start is called before the first frame update
    void Start()
    {        
    }

    // Update is called once per frame
    void Update()
    {
        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
            Debug.Log("Is grounded?");
        }


        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);

        if (Input.GetButtonDown("Jump") && isGrounded)  
        {
            Debug.Log("Jump");
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);

        }

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);






    }
}

PlayerMovement.cs (1.3 KB)

Looks like your code is derived from a broken example in the official Unity docs.

I say this because I see the .Move() method being called twice.

I wrote about this before: the Unity example code in the API no longer jumps reliably.

If you call .Move() twice in one single frame, the grounded check may fail.

I reported it to Unity via their docs feedback in October 2020. Apparently it is still broken:

Here is a work-around:

I recommend you also go to that same documentation page and ALSO report that the code is broken.

When you report it, you are welcome to link the above workaround. One day the docs might get fixed.

If you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

That one has run, walk, jump, slide, crouch… it’s crazy-nutty!!

1 Like