Jumping only works some of the time on main player.

Hello.

I am new to Unity and C# so excuse my ignorance, but I am having trouble with implementing jumping on my main player.

When press Space, my charcter will only jump some of the time. Weirdly though my secondary player (A little cat) will do his animation everytime I press space. I have attached an image of this happening.

Here is my code for the Player:

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

public class MovementScript : MonoBehaviour
{

    public CharacterController controller;
    public new Transform camera;

    public Animator anim;
    public new Rigidbody rigidbody;

    public float speed = 6f;
    public float turnSmoothing = 0.1f;
    public float jumpHeight = 1.2f;

    float turnSmoothingVel;
    Transform target;

    public CinemachineFreeLook flCam;

    public float FOVMin = 15f;
    public float FOVMax = 60f;

    public float gravity = -12f;

    private Vector3 playerVelocity;

    void Start()
    {
        anim = GetComponent<Animator>();
        target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
        CinemachineCore.GetInputAxis = GetAxisCustom;
    }

    // Update is called once per frame
    void Update()
    {

        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");

        Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
      
        float scroll = Input.GetAxisRaw("Mouse ScrollWheel");
        if (scroll != 0f)
        {
            float fov = flCam.m_Lens.FieldOfView;

            fov += scroll * 10f;
            fov = Mathf.Clamp(fov, 15f, 60f);
            flCam.m_Lens.FieldOfView = fov;
        }
      
        if (direction.magnitude >= 0.1f)
        {
            anim.SetBool("isRunning", true);

            float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + camera.eulerAngles.y;
            float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothingVel, turnSmoothing);

            transform.rotation = Quaternion.Euler(0f, angle, 0f);
            Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;

            controller.Move(moveDirection.normalized * speed * Time.deltaTime);
        }

        else
        {
            anim.SetBool("isRunning", false);
        }
    }

    private void LateUpdate()
    {
        bool groundedPlayer = controller.isGrounded;
        if (groundedPlayer && playerVelocity.y < 0)
        {
            anim.SetBool("isJumping", false);
            playerVelocity.y = 0f;
        }

        if (Input.GetButtonDown("Jump") && groundedPlayer)
        {
            anim.SetBool("isJumping", true);
            playerVelocity.y += Mathf.Sqrt(jumpHeight * -3.0f * gravity);
        }

        playerVelocity.y += gravity * Time.deltaTime;
        controller.Move(playerVelocity * Time.deltaTime);
    }

    private void FixedUpdate()
    {
        // Vector3 direction = Vector3.zero;

        // direction.y += Physics.gravity.y * gravity;
        // controller.Move(direction * Time.deltaTime);
    }

    public float GetAxisCustom(string axisName)
    {
        if (axisName == "Mouse X")
        {
            if (Input.GetMouseButton(0) || Input.GetMouseButton(1))
            {
                return UnityEngine.Input.GetAxis("Mouse X");
            }
            else
            {
                return 0;
            }
        }
        else if (axisName == "Mouse Y")
        {
            if (Input.GetMouseButton(0) ||Input.GetMouseButton(1))
            {
                return UnityEngine.Input.GetAxis("Mouse Y");
            }
            else
            {
                return 0;
            }
        }
        return UnityEngine.Input.GetAxis(axisName);
    }
}

Jumping Image:

Nothing in particular jumps out at me other than the fact you’re calling Move twice in the same frame, which you’re not supposed to do (see warning here). You also have a Rigidbody exposed, and you’re not supposed to use a Rigidbody and a CharacterController together either.

Other than those two potential issues, start throwing in Debug.Log statements and making sure your grounded and velocity values are what you think they are and start debugging from there.

Using Move twice in one frame definitely will break the ground testing portion of the CC.

I wrote about this before: the Unity example code in the API no longer jumps reliably. I have reported it. Here is a work-around: