Search Unity

Jumping with Character Controller Help Needed.

Discussion in 'Scripting' started by Mikeramczyk1977, Apr 4, 2021.

  1. Mikeramczyk1977

    Mikeramczyk1977

    Joined:
    Jul 12, 2020
    Posts:
    58
    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.


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5. using UnityEngine;
    6. using UnityEngine.XR;
    7.  
    8. [RequireComponent(typeof(Rigidbody))]
    9. [RequireComponent(typeof(CapsuleCollider))]
    10. public class NewJumpInput : MonoBehaviour
    11. {
    12.     [Header("Behaviour Options")]
    13.  
    14.  
    15.     [SerializeField]
    16.     public float jumpForce = 100.0f;
    17.  
    18.     [SerializeField]
    19.     private XRNode controllerNode = XRNode.RightHand;
    20.  
    21.     [SerializeField]
    22.     private bool checkForGroundOnJump = false;
    23.  
    24.     [Header("Capsule Collider Options")]
    25.     [SerializeField]
    26.     private Vector3 capsuleCenter = new Vector3(0, 1, 0);
    27.  
    28.     [SerializeField]
    29.     private float capsuleRadius = 0.3f;
    30.  
    31.     [SerializeField]
    32.     private float capsuleHeight = 1.6f;
    33.  
    34.     [SerializeField]
    35.     private CapsuleDirection capsuleDirection = CapsuleDirection.YAxis;
    36.  
    37.     private InputDevice controller;
    38.  
    39.     private bool isGrounded;
    40.  
    41.     private bool buttonPressed;
    42.  
    43.     private Rigidbody rigidBodyComponent;
    44.  
    45.     private CapsuleCollider capsuleCollider;
    46.  
    47.     private List<InputDevice> devices = new List<InputDevice>();
    48.  
    49.     public AudioSource jumpSound;
    50.  
    51.     public float rotateSpeed = 3.0f;
    52.  
    53.     public bool isGrounded1;
    54.     public float NumberJumps = 0f;
    55.     public float MaxJumps = 2;
    56.  
    57.     //Character Controller Test
    58.     public float speed = 6.0f;
    59.     public float gravity = -9.8f;
    60.     private CharacterController _charController;
    61.     private float verticalVelocity;
    62.     //Character Controller Test
    63.  
    64.     public enum CapsuleDirection
    65.     {
    66.  
    67.         YAxis
    68.     }
    69.  
    70.     void OnEnable()
    71.     {
    72.         rigidBodyComponent = GetComponent<Rigidbody>();
    73.         capsuleCollider = GetComponent<CapsuleCollider>();
    74.  
    75.         //Character Controller Test
    76.         rigidBodyComponent.constraints = RigidbodyConstraints.FreezeRotation;
    77.         capsuleCollider.direction = (int)capsuleDirection;
    78.         capsuleCollider.radius = capsuleRadius;
    79.         capsuleCollider.center = capsuleCenter;
    80.         capsuleCollider.height = capsuleHeight;
    81.         //Character Controller Test
    82.  
    83.  
    84.  
    85.     }
    86.  
    87.     void Start()
    88.     {
    89.         GetDevice();
    90.         //test
    91.         _charController = GetComponent<CharacterController>();
    92.         //Character Controller Test
    93.     }
    94.  
    95.     private void GetDevice()
    96.     {
    97.         InputDevices.GetDevicesAtXRNode(controllerNode, devices);
    98.         controller = devices.FirstOrDefault();
    99.     }
    100.  
    101.     void Update()
    102.     {
    103.         if (controller == null)
    104.         {
    105.             GetDevice();
    106.  
    107.             //test
    108.             float deltaX = Input.GetAxis("Horizontal") * speed;
    109.             float deltaZ = Input.GetAxis("Vertical") * speed;
    110.             Vector3 movement = new Vector3(deltaX, 0, deltaZ);
    111.             movement = Vector3.ClampMagnitude(movement, speed);
    112.  
    113.             movement.y = gravity;
    114.  
    115.             movement *= Time.deltaTime;
    116.             movement = transform.TransformDirection(movement);
    117.             _charController.Move(movement);
    118.             //Character Controller Test
    119.         }
    120.  
    121.  
    122.         UpdateJump(controller);
    123.  
    124.        
    125.  
    126.     }
    127.  
    128.  
    129.  
    130.     void UpdateJump(InputDevice controller)
    131.     {
    132.  
    133.         if (NumberJumps > MaxJumps - 1)
    134.         {
    135.             isGrounded1 = false;
    136.         }
    137.  
    138.         bool buttonValue;
    139.  
    140.  
    141.         isGrounded = (Physics.Raycast((new Vector2(transform.position.x, transform.position.y + 8.0f)), Vector3.down, 10.0f));
    142.  
    143.         Debug.DrawRay((new Vector3(transform.position.x, transform.position.y + 8.0f, transform.position.z)), Vector3.down, Color.red, 1.0f);
    144.  
    145.         if (!isGrounded && checkForGroundOnJump)
    146.             return;
    147.  
    148.  
    149.         if (isGrounded1)
    150.         {
    151.  
    152.  
    153.             if (controller.TryGetFeatureValue(CommonUsages.primaryButton, out buttonValue) && buttonValue)
    154.             {
    155.                 verticalVelocity = jumpForce;
    156.  
    157.  
    158.  
    159.  
    160.                 //Character Controller Test
    161.                 Vector3 jumpVector = new Vector3(0, verticalVelocity, 0);
    162.                 _charController.Move(jumpVector * Time.deltaTime);
    163.                 //Character Controller Test
    164.  
    165.  
    166.                 if (!buttonPressed)
    167.                 {
    168.  
    169.  
    170.                     Debug.Log("primaryButton is pressed " + buttonValue);
    171.                     buttonPressed = true;
    172.                     //rigidBodyComponent.AddForce((transform.up) * jumpForce);
    173.  
    174.                     //Character Controller Test
    175.                    
    176.                     //Character Controller Test
    177.                     jumpSound.Play();
    178.                    
    179.                     NumberJumps += 1;
    180.  
    181.                    
    182.  
    183.  
    184.                 }
    185.                
    186.                
    187.             }
    188.  
    189.  
    190.  
    191.  
    192.  
    193.             else if (buttonPressed)
    194.             {
    195.  
    196.                 Debug.Log("primaryButton is released " + buttonValue);
    197.                 buttonPressed = false;
    198.             }
    199.         }
    200.     }
    201.  
    202.     void OnCollisionEnter(Collision other)
    203.     {
    204.         isGrounded1 = true;
    205.         NumberJumps = 0;
    206.     }
    207.     void OnCollisionExit(Collision other)
    208.     {
    209.  
    210.     }
    211. }
    212.  
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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:

    https://forum.unity.com/threads/how...racter-movement-in-unity.981939/#post-6379746