Search Unity

localScale is not working at the runtime after applying the Animator component

Discussion in 'Scripting' started by draxex, Jul 6, 2022.

  1. draxex

    draxex

    Joined:
    May 12, 2016
    Posts:
    3
    I apply an animator component to my Game Object. After that when I play the game, I try to change the localScale of the game object but it doesn't work.



    Here is my code to change the game object's localScale according to its direction on the x coordinate.

    Code (CSharp):
    1. private Rigidbody2D rb;
    2. private Vector3 direction;
    3. private Animator anim;
    4. private bool isLookingRight;
    5.  
    6. void Start()
    7. {
    8.     rb = GetComponent<Rigidbody2D>();
    9.     anim = GetComponent<Animator>();
    10.     isLookingRight = false;
    11. }
    12.  
    13. private void FixedUpdate()
    14. {
    15.     float horizontal = Input.GetAxis("Horizontal");
    16.     TurnDirection(horizontal);
    17. }
    18.  
    19. // turns the player towards to movement direction
    20. private void TurnDirection(float horizontal)
    21. {
    22.     if (horizontal > 0 && !isLookingRight || horizontal < 0 && isLookingRight)
    23.     {
    24.         isLookingRight = !isLookingRight;
    25.         direction = transform.localScale;
    26.         direction.x *= -1;
    27.         transform.localScale = direction;
    28.     }
    29. }
    It worked well before applying the animation to the Game Object. I can't change the scale of the Game Object even manually on the inspector while playing the game.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. draxex

    draxex

    Joined:
    May 12, 2016
    Posts:
    3