- Home /
Delayed response: Jumping up from ledge
So I try to do some research before posting and I was able to get my player to jump up and grab onto a ledge.
I'm making a 2D platformer and there is a delay in the player jumping up or away from ledge from when I press the jump button. He sort of glitches there for a half second before jumping. I linked a example video here.
And the code is here. It was incredibly messy but I tried to clean it up for you. In the end there will be a charge jump feature but I removed it while I'm trying to figure out this delayed jumping issue. It is only while hanging and not a problem while grounded.
 using UnityEngine;
 using System.Collections;
 
 public class CoopMech : MonoBehaviour
 {
     [HideInInspector]
     public bool facingRight = true;
 
     //input
     float horiz;
     float vert;
     //movemnet
     Rigidbody2D rb;
     public float moveForce;
     public float maxSpeed = 5f;
     //Jumping
     public Transform groundCheckA;
     public Transform groundCheckB;
     public float groundRadius = 1f;
     public LayerMask groundLayerMask;
     bool grounded = false;
     //    bool canJump = false;
     //    bool ledgeHit = false;
     Vector2 ledgeRayDirection;
     LayerMask LedgeLayerMask;
     Transform ledgeCheck;
     bool hanging = false;
     public float rayLength = 1.5f;
     public float ledgeGrabRadius = 2.0f;
     public float curJumpForce;
     public float jumpChargeRate = 100;
     public float minJumpForce = 400f;
     public float maxJumpForce = 1000f;
     public float dashForce = 1000f;
     public Animator anim;
 
     void Awake ()
     { 
         print ("Hello");
         //initialize rigidbody, animator, ledgeCheck Transform, direction and layermask
         rb = gameObject.GetComponent<Rigidbody2D> ();
         anim = gameObject.GetComponent<Animator> ();
         ledgeCheck = transform.FindChild ("ledgeCheck");
 //        ledgeRayDirection = ledgeCheck.position - gameObject.transform.position;
         LedgeLayerMask = 1 << LayerMask.NameToLayer ("Ledge");
     }
 
     void Update ()
     {
         GroundCheck ();
         if (TouchControlsKit.TCKInput.GetButtonDown ("Jump") && grounded) {
             GroundJump ();
         } 
         if (TouchControlsKit.TCKInput.GetButtonDown ("Jump") && hanging) {
             HangingJump (curLedge);
         }
         print ("hanging is " + hanging);
     }
 
     void FixedUpdate ()
     {
         if (!hanging) {
             Movement ();
         }
         print (rb.velocity + "is the rb velocity (x, y)");
     }
 
     //---------------------Movement---------------------------------------------------------------------------------------------------------------------------------
 
     void Movement ()
     {
         //horizontal input
         horiz = TouchControlsKit.TCKInput.GetAxis ("DPad", "Horizontal");
         //movement: add force with horizontal axis
         if (horiz * rb.velocity.x < maxSpeed) {
             rb.AddForce (Vector2.right * horiz * moveForce);
             //            print (horiz);
         }
 
         //        greater than the maxSpeed reset to max speed
         if (Mathf.Abs (GetComponent<Rigidbody2D> ().velocity.x) > maxSpeed) {
             // ... set the player's velocity to the maxSpeed in the x axis.
             GetComponent<Rigidbody2D> ().velocity = new Vector2 (Mathf.Sign (GetComponent<Rigidbody2D> ().velocity.x) * maxSpeed, GetComponent<Rigidbody2D> ().velocity.y);
         }
 
         //set Animation parameters
         anim.SetFloat ("speed", Mathf.Abs (rb.velocity.x));
         anim.SetFloat ("yVel", rb.velocity.y);
         //        print ("velocity: " + rb.velocity);
 
         //FlippingThe Player
         if (horiz > 0 && !facingRight) {
             // ... flip the player.
             Flip ();
         }
         // Otherwise if the input is moving the player left and the player is facing right...
         else if (horiz < 0 && facingRight) {            // ... flip the player.
             Flip ();    
         }
     }
 
     void Flip ()
     {
         // Switch the way the player is labelled as facing.
         facingRight = !facingRight;
 
         // Multiply the player's x local scale by -1.
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     //----------------------------------------------GroundCheck------------------------------------------------------------------
 
     void GroundCheck ()
     {
         // Boolean GroundCheck
         grounded = Physics2D.OverlapArea (groundCheckA.position, groundCheckB.position, groundLayerMask);
         //NotGrounded
         LedgeDetection ();
         if (!grounded) {
             anim.SetBool ("grounded", false);
         } else {
             //grounded
             anim.SetBool ("grounded", true);
         }
     }
 
     //-----------------------Jumping--------------------
 
 //    public void ChargeJump ()
 //    {
 //        if (curJumpForce >= maxJumpForce) {
 //            curJumpForce = maxJumpForce;
 //        } else if (curJumpForce < maxJumpForce) {
 //            curJumpForce = curJumpForce + (jumpChargeRate * Time.deltaTime);
 //        }
 //    }
 
     void GroundJump ()
     {
         rb.AddForce (Vector2.up * curJumpForce);
     }
 
     void HangingJump (GameObject go)
     {
         print ("trying to jump2");
         go.SetActive (false);
         rb.constraints = RigidbodyConstraints2D.FreezeRotation;
         rb.AddForce (Vector2.up * maxJumpForce * 1.5f);
         StartCoroutine (LedgeReset (go));
     }
 
 
     //--------------------------------------------Ledge Detection and Grab------------------------------------------------------------
 
     public float ledgePosOffsetX = 2;
     public float ledgePosOffsetY = 5f;
     public float moveToLedgeTime = 0.25f;
     Vector3 playerPos;
     GameObject curLedge;
     BoxCollider2D curLedgeBC;
     Vector3 ledgePos;
     RaycastHit2D ledgeHit;
 
     void LedgeDetection ()
     {
         Vector3 rayDirection = (ledgeCheck.position - transform.position).normalized;
         ledgeHit = Physics2D.Raycast (gameObject.transform.position, rayDirection, rayLength, LedgeLayerMask);
         Debug.DrawRay (gameObject.transform.position, rayDirection * rayLength, Color.red);
 
         if (!ledgeHit) {
 //            rb.constraints = RigidbodyConstraints2D.None;
             rb.constraints = RigidbodyConstraints2D.FreezeRotation;
             hanging = false;
             anim.SetBool ("hanging", false);
         } else {
 //            print ("ledge hit");
             curLedge = ledgeHit.collider.gameObject as GameObject;
 //            curLedgeBC = ledgeHit.collider.transform.GetComponent<BoxCollider2D> ();
             ledgePos = ledgeHit.collider.transform.position;
             //            print ("ledgePos = " + ledgePos);
             //make player kinematic
 //            if (!grounded) {
             rb.constraints = RigidbodyConstraints2D.FreezeAll;
             //lerp to position.
             Vector3 newPos;
             if (facingRight) {
                 newPos = ledgePos - new Vector3 (ledgePosOffsetX, ledgePosOffsetY, 0.01f);
             } else {
                 newPos = ledgePos - new Vector3 (-ledgePosOffsetX, ledgePosOffsetY, 0.01f);
             }
             //            print ("NewPos = " + newPos);
             StartCoroutine (MoveToLedge (newPos));
             anim.SetBool ("hanging", true);
             //set HangingToTrue
 //            }
         }
     }
 
 
     IEnumerator MoveToLedge (Vector3 np)
     {
         playerPos = gameObject.transform.position;
         float startTime = Time.time;
         while (Time.time - startTime <= moveToLedgeTime) {
             transform.position = Vector3.Lerp (playerPos, np, 1);
             yield return 1;
         }
         hanging = true;
         yield break;
     }
 
     IEnumerator LedgeReset (GameObject go)
     {
         yield return new WaitForSeconds (2f);
         go.SetActive (true);
     }
 
 }
 
Your answer
 
 
             Follow this Question
Related Questions
Can only move character left or right, no foward or backwards. 1 Answer
How do I prevent my fps player from flying? 1 Answer
[SOLVED] 2D Character Controller gains velocity when colliding with a corner 1 Answer
Help please in regards to character controller 0 Answers
Shift BHOP in Character Controller 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                