Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Jump AddForce doesnt work when holding?

Discussion in 'Physics' started by thecoolman768210, May 6, 2024.

  1. thecoolman768210

    thecoolman768210

    Joined:
    Feb 28, 2024
    Posts:
    2
    I am making a character controller and the left and right movement works but if I hold the jump button and after I land land it runs the AddForce function but doesn't move up and I have to wait for another 0.25 seconds to jump.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using DG.Tweening;
    5.  
    6. public class PlayerController : MonoBehaviour
    7. {
    8.     public Rigidbody2D RigidBody;
    9.     public CapsuleCollider2D CapsuleCollider;
    10.  
    11.     public GameObject debugObject;
    12.  
    13.     public SpriteRenderer Sprite;
    14.  
    15.     private bool jumpCooldown = false;
    16.  
    17.     // costomisation (woahhh)
    18.  
    19.     public float speed = 3f;
    20.  
    21.     public float jumpHeight = 300f;
    22.  
    23.     // Start is called before the first frame update
    24.     void Start()
    25.     {
    26.  
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         RaycastHit2D NewRay = getGround();
    33.  
    34.         Movement(NewRay);
    35.  
    36.         capSpeed();
    37.     }
    38.     IEnumerator setJumpCooldown()
    39.     {
    40.         jumpCooldown = true;
    41.         yield return new WaitForSeconds(0.25f);
    42.         jumpCooldown = false;
    43.     }
    44.     void Movement(RaycastHit2D NewRay)
    45.     {
    46.         if (!jumpCooldown && NewRay.collider && Input.GetKey(KeyCode.Space))
    47.         {
    48.             StartCoroutine("setJumpCooldown");
    49.             RigidBody.drag = 0;
    50.             RigidBody.AddForce(Vector2.up * jumpHeight, ForceMode2D.Impulse);
    51.             Debug.Log("Jumped");
    52.         }
    53.         if (Input.GetKey(KeyCode.A))
    54.         {
    55.  
    56.             RigidBody.AddForce(Vector2.left * speed * 10);
    57.             transform.DORotate(new Vector3(transform.rotation.x, 180, transform.rotation.z), 1);
    58.         }
    59.         if (Input.GetKey(KeyCode.D))
    60.         {
    61.             RigidBody.AddForce(Vector2.right * speed * 10);
    62.             transform.DORotate(new Vector3(transform.rotation.x, 0, transform.rotation.z), 1);
    63.         }
    64.     }
    65.     RaycastHit2D getGround()
    66.     {
    67.         // Bit shift the index of the layer (8) to get a bit mask
    68.         int layerMask = 1 << 8;
    69.  
    70.         // This would cast rays only against colliders in layer 8, so we just inverse the mask.
    71.         layerMask = ~layerMask;
    72.         Vector2 Middle = (Vector2)transform.position + (CapsuleCollider.offset * 6);
    73.         RaycastHit2D NewRay = Physics2D.Raycast(Middle, Vector2.down, CapsuleCollider.size.y * 3f + 0.075f, layerMask);
    74.         debugObject.transform.position = Middle + Vector2.down * (CapsuleCollider.size.y * 3f + 0.075f);
    75.         Debug.Log(NewRay.collider);
    76.         if (NewRay.collider)
    77.         {
    78.             RigidBody.drag = 10;
    79.         }
    80.         else
    81.         {
    82.             RigidBody.drag = 0;
    83.         }
    84.         return NewRay;
    85.     }
    86.     void capSpeed()
    87.     {
    88.         Vector2 speedX = new Vector2(RigidBody.velocity.x,0);
    89.         if (speedX.magnitude > speed)
    90.         {
    91.             RigidBody.velocity = new Vector2((speedX.normalized * speed).x, RigidBody.velocity.y);
    92.         }
    93.     }
    94. }
    95.  
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    1,054
    Your raycast detects the ground before the character has actually landed and come to a stop and so your character still has lots of downwards velocity which means your AddForce is no longer enough to make the character jump. Your script then has to wait 0.25 seconds before attempting to jump again.

    Change line 50 to:
    Code (CSharp):
    1. RigidBody.AddForce(Vector2.up * (jumpHeight-RigidBody.velocity.y), ForceMode2D.Impulse);
    This will compensate for the character's current velocity when jumping.