Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sprite reversing not working

Discussion in 'Scripting' started by matias-e, Aug 18, 2015.

  1. matias-e

    matias-e

    Joined:
    Sep 29, 2014
    Posts:
    106
    Hey! I have a script that flips the player around to face the correct direction, but it's not quite working. When I touch a movable and start pushing, the player turns around for a blink and then flips back to the wrong direction. The problem is that when the player hits the movable's collider the collider enters it twice and exits it once. Why does the collider enter twice? Here's the script I have:

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D collision) {
    2.  
    3.         canJump = true;
    4.  
    5.         if(collision.collider.tag == "Movable") {
    6.             pushing = true;
    7.             TurnPlayerAround();
    8.         }
    9.     }
    10.  
    11.     void OnCollisionExit2D(Collision2D collision) {
    12.  
    13.         if(collision.collider.tag == "Ground") {
    14.             canJump = false;
    15.         }
    16.  
    17.         if(collision.collider.tag == "Movable") {
    18.             Debug.Log("WentToExit");
    19.             pushing = false;
    20.             canJump = true;
    21.         }
    22.     }
    23.  
    24.     void TurnPlayerAround () {
    25.         scale.x *= -1;
    26.         transform.localScale = scale;
    27.   }
    28.        
     
    Last edited: Aug 18, 2015