Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kufskr · Sep 28, 2016 at 03:23 PM · c#2d-platformercharacter controllercharacter movementglitching

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);
     }
 
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges