Search Unity

Bug Player Keeps Bouncing Off Corner of Platforms/Blocks PLEASE HELP

Discussion in '2D' started by BitByBitLab, May 7, 2023.

  1. BitByBitLab

    BitByBitLab

    Joined:
    Feb 28, 2023
    Posts:
    1
    I have spent about 20 hours trying to fix this and tried just about everything at this point. I am using a Tilemap and using a Tilemap collider and and a composite Collider with it.
    I have a Dash on my character and when it lands on the corner of the blocks/platforms the player bounces off and for some reason it doesn't detect a collision. I added a Physics Material with no friction and no bounce to just about everything and it still happens.

    I tried to upload a gyf to gyfcat but it did not work so here is the link to the clip: https://clipchamp.com/watch/O6C9n3KWwpa

    Here is the code for the movement:
    Code (CSharp):
    1.     void Update()
    2.     {
    3.         movementDir = Input.GetAxis("Horizontal");
    4.         if (Input.GetKey("space") && isGrounded)
    5.         {
    6.             jump = true;
    7.         }
    8.         if (rb.velocity.y > 0f)
    9.         {
    10.             rb.gravityScale = 1.5f;
    11.         }
    12.         else if (rb.velocity.y < 0f)
    13.         {
    14.             rb.gravityScale = 2.5f;
    15.         }
    16.     }
    17.  
    18.     private void FixedUpdate()
    19.     {
    20.         movementHandler();
    21.  
    22.     }
    23.  
    24.     private void movementHandler()
    25.     {
    26.         if (!playerPowers.hasDashed)
    27.         {
    28.  
    29.             rb.velocity = new Vector2(movementDir * speed * Time.deltaTime, rb.velocity.y);
    30.             playervelocity = rb.velocity;
    31.             if (movementDir > 0 && flag)
    32.             {
    33.                 playerTransform.Rotate(Vector3.up, 180f);
    34.                 flag = false;
    35.             }
    36.             else if (movementDir < 0 && !flag)
    37.             {
    38.                 playerTransform.Rotate(Vector3.up, -180f);
    39.                 flag = true;
    40.             }
    41.             if (jump)
    42.             {
    43.                 isGrounded = false;
    44.                 jump = false;
    45.                 Vector2 jumpVector = new Vector2(0f, jumpForce * Time.deltaTime);
    46.                 rb.AddForce(jumpVector);
    47.             }
    48.         }
    49.         else
    50.         {
    51.             rb.velocity = rb.velocity;
    52.         }
    53.  
    54.     }
    Here is the code for the collision detection:
    Code (CSharp):
    1.     private void OnTriggerStay2D(Collider2D collision)
    2.     {
    3.         if (collision.gameObject.tag == "Ground" && rb.velocity.y > -1f && rb.velocity.y < 1f)
    4.         {
    5.             playerMovement.isGrounded = true;
    6.         }
    7.     }
    8.     private void OnTriggerEnter2D(Collider2D collision)
    9.     {
    10.         if (playerPowers.hasDashed)
    11.         {
    12.             Debug.Log("Collided");
    13.         }
    14.         if (collision.gameObject.tag == "Ground")
    15.         {
    16.             playerPowers.hasDashed = false;
    17.         }
    18.     }
    Here is the code for the Dash:
    Code (CSharp):
    1.     // Update is called once per frame
    2.     void Update()
    3.     {
    4.         checkDash();
    5.     }
    6.  
    7.     private void FixedUpdate()
    8.     {
    9.         if (dash)
    10.         {
    11.             dashHandler();
    12.         }
    13.     }
    14.  
    15.     private void checkDash()
    16.     {
    17.         if (Input.GetKey("e") && !playerMovement.isGrounded && !hasDashed)
    18.         {
    19.             dash = true;
    20.             hasDashed = true;
    21.             stopMovement = true;
    22.         }
    23.         if (playerMovement.isGrounded)
    24.         {
    25.             hasDashed = false;
    26.         }
    27.  
    28.         if (playerTransform.rotation.y == 1)
    29.         {
    30.             dashDirection = 1;
    31.             dashRotation = 270f;
    32.         }
    33.         else
    34.         {
    35.             dashDirection = -1;
    36.             dashRotation = 90f;
    37.         }
    38.     }
    39.     private void dashHandler()
    40.     {
    41.         Vector3 dashLocationPos = new Vector3(dashLocation.position.x, dashLocation.position.y, 0f);
    42.         Instantiate(dashAnimation, dashLocationPos, Quaternion.Euler(0f, 0f, dashRotation));
    43.         rb.AddForce(new Vector2(dashForce * dashDirection * Time.deltaTime, rb.velocity.y + dashY *Time.deltaTime));
    44.         dash = false;
    45.     }
    I have done just about everything for the collision detection including using raycasts and boxcasts but the issue persisted.

    I have done collision handling in FixedUpdate and OnTriggerEnter and it still does it.

    I also realized that when this happens the OnTriggerEnter function does not get called.

    I made my dash in a way where if you dash, you won't be able to move hence why I have the if statement on my playerMovement script.

    Please help me I'm reaching my limit.
     
    Last edited: May 7, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    I don't follow and the video is so fast without context of what I'm looking at. I don't understand why a trigger is supposed to cause a collision response as it won't. The fragmented snippets above just show a whole load of logic. I am unable to debug that in my head without context.

    You can pass all the way through a collider with a trigger, it doesn't have any collision response. If the trigger callbacks is not being called then whatever was supposed to be interacting with it either didn't touch it or stepped over it in a single simulation step. I don't see how a physics material relates to using a trigger, there is no collision response.

    I think you just need to debug this and provide more context.
     
    Kurt-Dekker likes this.