Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question While wall jumping, I'm losing the key down (it seems) in 2D

Discussion in '2D' started by invasionofsmallcubes, Mar 27, 2021.

  1. invasionofsmallcubes

    invasionofsmallcubes

    Joined:
    Mar 7, 2021
    Posts:
    5
    Hi, this is an example.

    I'm totally new to Unity. I tried to split the code in a way that makes sense using both `Update` and `FixedUpdate` where I keep the stuff regarding the physics.

    I'm using the new input system as well. As you can see sometimes I cannot jump while on the wall, sometimes the jump kicks in later (like you see at the end of the video where I jump from the ground).

    I'm not able to understand why. Any help would be appreciated. Here's the source code:

    Code (CSharp):
    1.   using UnityEngine;
    2.     using UnityEngine.InputSystem;
    3.     using UnityEngine.UI;
    4.  
    5.     public class PlayerController : MonoBehaviour
    6.     {
    7.         public Rigidbody2D rigidBody;
    8.         public float moveSpeed, jumpForce;
    9.         public Transform groundCheckPoint;
    10.         public LayerMask whatisGround;
    11.         public Animator anim;
    12.         public Transform wallGrabPoint;
    13.         public Text text;
    14.  
    15.         private bool isGrounded;
    16.         private bool isGrabbing;
    17.         private bool canGrab;
    18.         private Vector2 move;
    19.         private float originalGravity;
    20.         private bool isJumping;
    21.  
    22.         private void Awake()
    23.         {
    24.             move = Vector2.zero;
    25.             originalGravity = rigidBody.gravityScale;
    26.         }
    27.  
    28.         public void OnJump()
    29.         {
    30.             if (isGrounded || canGrab)
    31.             {
    32.                 isJumping = true;
    33.             }
    34.         }
    35.  
    36.         public void OnMove(InputValue input)
    37.         {
    38.             move = input.Get<Vector2>();
    39.         }
    40.  
    41.         void FixedUpdate()
    42.         {
    43.             isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, whatisGround);
    44.             canGrab = Physics2D.OverlapCircle(wallGrabPoint.position, .2f, whatisGround);
    45.  
    46.             rigidBody.velocity = new Vector2(move.x * moveSpeed, rigidBody.velocity.y);
    47.  
    48.             if (isJumping && !canGrab)
    49.             {
    50.                 rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpForce);
    51.                 isJumping = false;
    52.             }
    53.  
    54.             isGrabbing = false;
    55.             if (canGrab && !isGrounded)
    56.             {
    57.                 if ( (transform.localScale.x == 1f && move.x > 0) ||
    58.                     (transform.localScale.x == -1f && move.x < 0) )
    59.                 {
    60.                     isGrabbing = true;
    61.                     rigidBody.velocity = Vector2.zero;
    62.                     if (isJumping)
    63.                     {
    64.                         var force = new Vector2(-250*(move.x * moveSpeed), 50*jumpForce);
    65.                         rigidBody.AddForce(force);
    66.                         isJumping = false;
    67.                     }
    68.                 } else
    69.                 {
    70.                     rigidBody.gravityScale = originalGravity;
    71.                 }
    72.             }
    73.  
    74.         }
    75.  
    76.         private void Update()
    77.         {
    78.             FlipDirection();
    79.  
    80.             anim.SetFloat("speed", Mathf.Abs(rigidBody.velocity.x));
    81.             anim.SetBool("isGrounded", isGrounded);
    82.             anim.SetBool("isGrabbing", isGrabbing);
    83.             UpdateStatus();
    84.         }
    85.  
    86.         private void FlipDirection()
    87.         {
    88.             if (rigidBody.velocity.x > 0)
    89.             {
    90.                 transform.localScale = Vector3.one;
    91.             }
    92.             else if (rigidBody.velocity.x < 0)
    93.             {
    94.                 transform.localScale = new Vector3(-1f, 1, 1f);
    95.             }
    96.         }
    97.  
    98.         void UpdateStatus()
    99.         {
    100.             text.text = string.Format("Velocity = {0}" +
    101.                 "\ntransform = {1}" +
    102.                 "\nisGrounded = {2}" +
    103.                 "\nisGrabbing = {3}" +
    104.                 "\nisJumping = {4}" +
    105.                 "\nmove = {5}",
    106.                 rigidBody.velocity.ToString("F0"),
    107.                 transform.localScale.ToString("F0"),
    108.                 isGrounded.ToString(),
    109.                 isGrabbing.ToString(),
    110.                 isJumping.ToString(),
    111.                 move.ToString("F0"));
    112.         }
    113.     }
    114.  
    Any help is appreciated.

    Thanks.
     
  2. invasionofsmallcubes

    invasionofsmallcubes

    Joined:
    Mar 7, 2021
    Posts:
    5
    Hi, I was able to fix part of it, as you can see from here.

    This is the current source code:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.     public Rigidbody2D rigidBody;
    8.     public float moveSpeed = 12, jumpWallSpeed = 6, jumpForce = 20, jumpWallTimeout = 0.02f, jumpWallForce = 5;
    9.     public Transform groundCheckPoint;
    10.     public LayerMask whatisGround;
    11.     public Animator anim;
    12.     public Transform wallGrabPoint;
    13.     public Text statusText;
    14.     public Text actionText;
    15.  
    16.  
    17.     private bool isGrounded;
    18.     private bool isGrabbing;
    19.     private bool canGrab;
    20.     private Vector2 move;
    21.     private float originalGravity;
    22.     private bool isWallJumping;
    23.  
    24.     private bool jumpPressed;
    25.     private bool movePressed;
    26.  
    27.     private void Awake()
    28.     {
    29.         move = Vector2.zero;
    30.         originalGravity = rigidBody.gravityScale;
    31.     }
    32.  
    33.     public void OnJump()
    34.     {
    35.         jumpPressed = true;
    36.     }
    37.  
    38.     public void OnMove(InputValue input)
    39.     {
    40.         movePressed = true;
    41.         move = input.Get<Vector2>();
    42.     }
    43.  
    44.     void FixedUpdate()
    45.     {
    46.         isGrounded = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, whatisGround);
    47.  
    48.         if (!jumpPressed && movePressed && isGrounded)
    49.         {
    50.             actionText.text = "IS MOVING";
    51.             rigidBody.velocity = new Vector2(move.x * moveSpeed, rigidBody.velocity.y);
    52.             movePressed = false;
    53.         }
    54.  
    55.         if (jumpPressed && !movePressed && isGrounded)
    56.         {
    57.             actionText.text = "IS JUMPING";
    58.             rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpForce);
    59.             jumpPressed = false;
    60.         }
    61.  
    62.         if (jumpPressed && movePressed && canGrab)
    63.         {
    64.             isWallJumping = true;
    65.             jumpPressed = false;
    66.             movePressed = false;
    67.         } else
    68.         {
    69.             canGrab = Physics2D.OverlapCircle(wallGrabPoint.position, .2f, whatisGround);
    70.             isGrabbing = false;
    71.             if (canGrab && !isGrounded)
    72.             {
    73.                 if ((transform.localScale.x == 1f && move.x > 0) ||
    74.                     (transform.localScale.x == -1f && move.x < 0))
    75.                 {
    76.                     actionText.text = "IS GRABBING";
    77.                     isGrabbing = true;
    78.                     rigidBody.velocity = Vector2.zero;
    79.                     rigidBody.gravityScale = 0f;
    80.                     jumpPressed = false;
    81.                 }
    82.                 else
    83.                 {
    84.                     actionText.text = "NOT GRABBING";
    85.                     rigidBody.gravityScale = originalGravity;
    86.                     jumpPressed = false;
    87.                 }
    88.             }
    89.         }
    90.        
    91.         if (isWallJumping)
    92.         {
    93.             actionText.text = "IS WALLJUMPING";
    94.             rigidBody.velocity = new Vector2(move.x * jumpWallSpeed, jumpWallForce);
    95.             Invoke(nameof(SetWallJumpingToFalse), jumpWallTimeout);
    96.             rigidBody.gravityScale = originalGravity;
    97.             jumpPressed = false;
    98.             movePressed = false;
    99.         }
    100.  
    101.         if (jumpPressed && movePressed)
    102.         {
    103.             jumpPressed = false;
    104.         }
    105.     }
    106.  
    107.     private void SetWallJumpingToFalse()
    108.     {
    109.         isWallJumping = false;
    110.     }
    111.  
    112.     private void Update()
    113.     {
    114.         FlipDirection();
    115.  
    116.         anim.SetFloat("speed", Mathf.Abs(rigidBody.velocity.x));
    117.         anim.SetBool("isGrounded", isGrounded);
    118.         anim.SetBool("isGrabbing", isGrabbing);
    119.  
    120.         UpdateStatusText();
    121.     }
    122.  
    123.     private void FlipDirection()
    124.     {
    125.         if (rigidBody.velocity.x > 0)
    126.         {
    127.             transform.localScale = Vector3.one;
    128.         }
    129.         else if (rigidBody.velocity.x < 0)
    130.         {
    131.             transform.localScale = new Vector3(-1f, 1, 1f);
    132.         }
    133.     }
    134.  
    135.     private void UpdateStatusText()
    136.     {
    137.         statusText.text = string.Format("Status\n" +
    138.             "isWallJumping: <b>{0}</b>\n" +
    139.             "isGrounded: <b>{1}</b>\n" +
    140.             "move: <b>{2}</b>\n" +
    141.             "isGrabbing: <b>{3}</b>\n" +
    142.             "velocity: <b>{4}</b>\n" +
    143.             "jumpPressed: <b>{5}</b>\n" +
    144.             "movePressed: <b>{6}</b>\n" +
    145.             "localScale: <b>{7}</b>",
    146.             isWallJumping.ToString(),
    147.             isGrounded.ToString(),
    148.             move.ToString("F0"),
    149.             isGrabbing.ToString(),
    150.             rigidBody.velocity.ToString("F0"),
    151.             jumpPressed,
    152.             movePressed,
    153.             transform.localScale.ToString("F0"));
    154.     }
    155. }
    If you notice in the last part of the gif, the player cannot jump when close to the wall, and if you notice the sprites get flipped when going close to the wall.

    And I don't understadn why.