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
  3. Dismiss Notice

Bug character jump near on the wall

Discussion in 'Tizen' started by Pandausers, Feb 20, 2024.

  1. Pandausers

    Pandausers

    Joined:
    Feb 17, 2024
    Posts:
    36
    I want to say that when the character is unhooked from the wall, he has a few milliseconds to jump from nothing and I don't understand what the problem is, pls help

    sorry for bad video quality

    if you need more information, tell
    me
     

    Attached Files:

    • bug.gif
      bug.gif
      File size:
      384.7 KB
      Views:
      38
    Last edited: Feb 20, 2024
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,474
    Share your code (using code tags from the text ribbon, please). There is no way anyone can realistically help you without seeing what you are already doing.
     
  3. Pandausers

    Pandausers

    Joined:
    Feb 17, 2024
    Posts:
    36
    sure here code
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player : MonoBehaviour
    4. {
    5.     private float horizontal;
    6.     private float speed = 8f;
    7.     private float jumpingPower = 16f;
    8.     private bool isFacingRight = true;
    9.  
    10.     private bool isWallSliding;
    11.     private float wallSlidingSpeed = 2f;
    12.  
    13.     private bool isWallJumping;
    14.     private float wallJumpingDirection;
    15.     private float wallJumpingTime = 0.2f;
    16.     private float wallJumpingCounter;
    17.     private float wallJumpingDuration = 0.2f;
    18.     private Vector2 wallJumpingPower = new Vector2(8f, 16f);
    19.  
    20.     [SerializeField] private Rigidbody2D rb;
    21.     [SerializeField] private Transform groundCheck;
    22.     [SerializeField] private LayerMask groundLayer;
    23.     [SerializeField] private Transform wallCheck;
    24.     [SerializeField] private LayerMask wallLayer;
    25.  
    26.     [SerializeField] private Animator anim;
    27.  
    28.     // Update is called once per frame
    29.     private void Update()
    30.     {
    31.         // Get horizontal input
    32.         horizontal = Input.GetAxisRaw("Horizontal");
    33.  
    34.         // Check for jump input and grounded state
    35.         if (Input.GetButtonDown("Jump") && IsGrounded())
    36.         {
    37.             // Apply upward force for jumping
    38.             rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
    39.         }
    40.  
    41.         // Check for wall sliding and wall jumping
    42.         WallSlide();
    43.         WallJump();
    44.  
    45.         // Flip the character if not wall jumping
    46.         if (!isWallJumping)
    47.         {
    48.             Flip();
    49.         }
    50.  
    51.         // Update animation parameters
    52.         SetAnimationParameters();
    53.     }
    54.  
    55.     // FixedUpdate is called at a fixed interval
    56.     private void FixedUpdate()
    57.     {
    58.         // Move the character horizontally
    59.         if (!isWallJumping)
    60.         {
    61.             rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    62.         }
    63.     }
    64.  
    65.     // Check if the player is grounded
    66.     private bool IsGrounded()
    67.     {
    68.         return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundLayer);
    69.     }
    70.  
    71.     // Check if the player is touching a wall
    72.     private bool IsWalled()
    73.     {
    74.         return Physics2D.OverlapCircle(wallCheck.position, 0.2f, wallLayer);
    75.     }
    76.  
    77.     // Handle wall sliding behavior
    78.     private void WallSlide()
    79.     {
    80.         // Check if the player is against a wall, not grounded, and moving horizontally
    81.         if (IsWalled() && !IsGrounded() && horizontal != 0f)
    82.         {
    83.             // Enable wall sliding and adjust vertical velocity
    84.             isWallSliding = true;
    85.             rb.velocity = new Vector2(rb.velocity.x, Mathf.Clamp(rb.velocity.y, -wallSlidingSpeed, float.MaxValue));
    86.         }
    87.         else
    88.         {
    89.             // Disable wall sliding
    90.             isWallSliding = false;
    91.         }
    92.     }
    93.  
    94.     // Handle wall jumping behavior
    95.     private void WallJump()
    96.     {
    97.         // Check if the player is wall sliding
    98.         if (isWallSliding)
    99.         {
    100.             // Set up wall jump parameters
    101.             isWallJumping = false;
    102.             wallJumpingDirection = -transform.localScale.x;
    103.             wallJumpingCounter = wallJumpingTime;
    104.  
    105.             // Cancel any previous wall jump calls
    106.             CancelInvoke(nameof(StopWallJumping));
    107.         }
    108.         else
    109.         {
    110.             // Decrease the wall jumping counter over time
    111.             wallJumpingCounter -= Time.deltaTime;
    112.         }
    113.  
    114.         // Check for jump input during wall jump preparation
    115.         if (Input.GetButtonDown("Jump") && wallJumpingCounter > 0.001f)
    116.         {
    117.             // Execute wall jump
    118.             isWallJumping = true;
    119.             rb.velocity = new Vector2(wallJumpingDirection * wallJumpingPower.x, wallJumpingPower.y);
    120.             wallJumpingCounter = 0.001f;
    121.  
    122.             // Flip the character if needed
    123.             if (transform.localScale.x != wallJumpingDirection)
    124.             {
    125.                 isFacingRight = !isFacingRight;
    126.                 Vector3 localScale = transform.localScale;
    127.                 localScale.x *= -1f;
    128.                 transform.localScale = localScale;
    129.             }
    130.  
    131.             // Stop wall jumping after a short duration
    132.             Invoke(nameof(StopWallJumping), wallJumpingDuration);
    133.         }
    134.     }
    135.  
    136.     // Stop wall jumping state
    137.     private void StopWallJumping()
    138.     {
    139.         isWallJumping = false;
    140.     }
    141.  
    142.     // Flip the character horizontally
    143.     private void Flip()
    144.     {
    145.         // Check for direction change and flip the character
    146.         if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
    147.         {
    148.             isFacingRight = !isFacingRight;
    149.             Vector3 localScale = transform.localScale;
    150.             localScale.x *= -1f;
    151.             transform.localScale = localScale;
    152.         }
    153.     }
    154.  
    155.     // Set animation parameters based on character state
    156.     private void SetAnimationParameters()
    157.     {
    158.         // Check for character movement and update animation parameters
    159.         bool isMoving = Mathf.Abs(rb.velocity.x) > 0.1f;
    160.         anim.SetFloat("yVelocity", rb.velocity.y);
    161.         anim.SetBool("isGrounded", IsGrounded());
    162.         anim.SetBool("isMoving", isMoving);
    163.         anim.SetBool("isWallSliding", isWallSliding);
    164.     }
    165. }
    166.  
     
  4. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    212
    The if statement for wall jumping only cares if wallJumpingCounter is greater than 0.001f.
    While isWallSliding is true, wallJumpingCounter gets set to wallJumpingTime (0.2f). When isWallSliding goes false, wallJumpingCounter starts to decrease. This means there will be 200ms after you leave a wall where a wall jump will still be allowed. This acts a bit like coyote time for wall sliding, but it doesn't sound like it is what you want. I think you should check for isWallSliding in statement on line 115.
     
  5. Pandausers

    Pandausers

    Joined:
    Feb 17, 2024
    Posts:
    36
    I already understood why this was happening, I just reduced the time to a few multimilliseconds in some places, and everything became normal
    I already understood why this was happening, I just reduced the time to a few multimilliseconds in some places, and everything became normal