Jumping with Character Controller Help Needed.

The issue is that my character only jumps to a full height and completes the jump so long as I am holding the button down, I want the character to jump to a set height and fall back down, like normal, upon button press, not press and hold. Any Help, would be greatly appreciated. I am trying to learn as I go.

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.XR;

[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class NewJumpInput : MonoBehaviour
{
    [Header("Behaviour Options")]


    [SerializeField]
    public float jumpForce = 100.0f;

    [SerializeField]
    private XRNode controllerNode = XRNode.RightHand;

    [SerializeField]
    private bool checkForGroundOnJump = false;

    [Header("Capsule Collider Options")]
    [SerializeField]
    private Vector3 capsuleCenter = new Vector3(0, 1, 0);

    [SerializeField]
    private float capsuleRadius = 0.3f;

    [SerializeField]
    private float capsuleHeight = 1.6f;

    [SerializeField]
    private CapsuleDirection capsuleDirection = CapsuleDirection.YAxis;

    private InputDevice controller;

    private bool isGrounded;

    private bool buttonPressed;

    private Rigidbody rigidBodyComponent;

    private CapsuleCollider capsuleCollider;

    private List<InputDevice> devices = new List<InputDevice>();

    public AudioSource jumpSound;

    public float rotateSpeed = 3.0f;

    public bool isGrounded1;
    public float NumberJumps = 0f;
    public float MaxJumps = 2;

    //Character Controller Test 
    public float speed = 6.0f;
    public float gravity = -9.8f;
    private CharacterController _charController;
    private float verticalVelocity;
    //Character Controller Test 

    public enum CapsuleDirection
    {

        YAxis
    }

    void OnEnable()
    {
        rigidBodyComponent = GetComponent<Rigidbody>();
        capsuleCollider = GetComponent<CapsuleCollider>();

        //Character Controller Test 
        rigidBodyComponent.constraints = RigidbodyConstraints.FreezeRotation;
        capsuleCollider.direction = (int)capsuleDirection;
        capsuleCollider.radius = capsuleRadius;
        capsuleCollider.center = capsuleCenter;
        capsuleCollider.height = capsuleHeight;
        //Character Controller Test 



    }

    void Start()
    {
        GetDevice();
        //test
        _charController = GetComponent<CharacterController>();
        //Character Controller Test
    }

    private void GetDevice()
    {
        InputDevices.GetDevicesAtXRNode(controllerNode, devices);
        controller = devices.FirstOrDefault();
    }

    void Update()
    {
        if (controller == null)
        {
            GetDevice();

            //test
            float deltaX = Input.GetAxis("Horizontal") * speed;
            float deltaZ = Input.GetAxis("Vertical") * speed;
            Vector3 movement = new Vector3(deltaX, 0, deltaZ);
            movement = Vector3.ClampMagnitude(movement, speed);

            movement.y = gravity;

            movement *= Time.deltaTime;
            movement = transform.TransformDirection(movement);
            _charController.Move(movement);
            //Character Controller Test
        }


        UpdateJump(controller);

       

    }



    void UpdateJump(InputDevice controller)
    {

        if (NumberJumps > MaxJumps - 1)
        {
            isGrounded1 = false;
        }

        bool buttonValue;


        isGrounded = (Physics.Raycast((new Vector2(transform.position.x, transform.position.y + 8.0f)), Vector3.down, 10.0f));

        Debug.DrawRay((new Vector3(transform.position.x, transform.position.y + 8.0f, transform.position.z)), Vector3.down, Color.red, 1.0f);

        if (!isGrounded && checkForGroundOnJump)
            return;


        if (isGrounded1)
        {


            if (controller.TryGetFeatureValue(CommonUsages.primaryButton, out buttonValue) && buttonValue)
            {
                verticalVelocity = jumpForce;




                //Character Controller Test
                Vector3 jumpVector = new Vector3(0, verticalVelocity, 0);
                _charController.Move(jumpVector * Time.deltaTime);
                //Character Controller Test


                if (!buttonPressed)
                {


                    Debug.Log("primaryButton is pressed " + buttonValue);
                    buttonPressed = true;
                    //rigidBodyComponent.AddForce((transform.up) * jumpForce);

                    //Character Controller Test
                   
                    //Character Controller Test
                    jumpSound.Play();
                   
                    NumberJumps += 1;

                   


                }
               
               
            }





            else if (buttonPressed)
            {

                Debug.Log("primaryButton is released " + buttonValue);
                buttonPressed = false;
            }
        }
    }

    void OnCollisionEnter(Collision other)
    {
        isGrounded1 = true;
        NumberJumps = 0;
    }
    void OnCollisionExit(Collision other)
    {

    }
}

If you want to debug this code, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

If you just want an example of the behavior you describe there are many. Here’s mine, derived off the Unity example code and fixed for their apparently-changed CC behavior:

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