Search Unity

Why is this Flip code not working?

Discussion in 'Scripting' started by AdamCNorton, Jul 31, 2014.

  1. AdamCNorton

    AdamCNorton

    Joined:
    Apr 16, 2013
    Posts:
    57
    Hey guys, can you help me spot why this code isn't working? I think it's something simple, but I have been looking at it for awhile and can't figure out what's up. So my character comes along to an object he's going to hide behind. That part is working. The trigger recognizes the player, identifies which size object it is, moves to the correct position, and plays the correct animation, just as expected. However, on some objects, I have a tag called "HideLeft". If that tag is present, I want to flip the animation to have him facing the other way. This is all 2D. The Debug text in my Flip() is displaying, so it does appear to be calling everything correctly. But the visual of the character does not flip.

    Is it possible that something in my Math.f step in the OnTriggerStay2D() is flipping it back? I thought since I was only changing position there that this should work. Can you see what's wrong?

    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D other)
    2.     {
    3.         if(other.gameObject.tag == "Player" && playerLoco.hidingSpot == gameObject)
    4.         {
    5.             playerLoco.move = playerLoco.stopped;
    6.             playerLoco.hidden = true;
    7.  
    8.             if(gameObject.tag == "HidingSpot_Large")
    9.             {
    10.                 playerLoco.anim.SetBool("HideLarge", true);
    11.             }
    12.             else if(gameObject.tag == "HidingSpot_Small")
    13.             {
    14.                 playerLoco.anim.SetBool("HideSmall", true);
    15.             }
    16.             else if(gameObject.tag == "HidingSpot_Under")
    17.             {
    18.                 playerLoco.anim.SetBool("HideUnder", true);
    19.             }
    20.  
    21.             if(hideTarget.tag == "HideLeft")
    22.             {
    23.                 Flip();
    24.  
    25.             }
    26.         }
    27.     }
    28.  
    29.     void OnTriggerStay2D(Collider2D other)
    30.     {
    31.         if(other.gameObject.tag == "Player" && playerLoco.hidingSpot == gameObject && playerLoco.hidden == true)
    32.         {
    33.             player.transform.position = new Vector2(Mathf.MoveTowards(player.transform.position.x, hideTarget.transform.position.x, 5.0f), hideTarget.transform.position.y);
    34.  
    35.         }
    36.     }
    37.  
    38.     public void Flip()
    39.     {
    40.         Vector3 theScale = player.transform.localScale;
    41.         theScale.x *= -1;
    42.         player.transform.localScale = theScale;
    43.         Debug.Log ("Did I flip?");
    44.     }
     
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Try this

    player.transorm.localScale = new Vector3(player.transform.localScale * 1.0f, player.transform.localScale.y, player.transform.localScale.z);