Search Unity

Bug sprite.flipX not working

Discussion in 'Editor & General Support' started by antonkk21, Mar 26, 2023.

  1. antonkk21

    antonkk21

    Joined:
    Mar 26, 2023
    Posts:
    2
    hi,
    I'm learning unity and working on a 2d platform game.
    sprite.flipX is not working, the log shows it updates the flipx value but it has no effect on the unity SpriteRenderer on my player inspector.
    player moving, jumping, jumping to the sides and all animations work correctly except the change direction of the right walking animation.
    Been searching online for 6 hours now and super frustrated, please help me figure out the problem.
    My code :

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     private Rigidbody2D rb;
    8.     private BoxCollider2D coll; //top platform collision -> line 26
    9.     private SpriteRenderer sprite;
    10.     private Animator anim;
    11.  
    12.     [SerializeField] private LayerMask jumpableGround; // for our new box colider, terrain layer pass
    13.  
    14.     private float dirX = 0f;
    15.     [SerializeField] private float moveSpeed = 7f;
    16.     [SerializeField] private float jumpForce = 14f;
    17.  
    18.     private enum MovementState { idle, walk, jumping, falling, side_jump_left, side_jump_right }
    19.  
    20.     [SerializeField] private AudioSource jumpSoundEffect;
    21.  
    22.     // Start is called before the first frame update
    23.     private void Start()
    24.     {
    25.         rb = GetComponent<Rigidbody2D>();
    26.         coll = GetComponent<BoxCollider2D>();
    27.         sprite = GetComponent<SpriteRenderer>();
    28.         anim = GetComponent<Animator>();
    29.     }
    30.  
    31.     // Update is called once per frame
    32.     private void Update()
    33.     {
    34.         dirX = Input.GetAxisRaw("Horizontal");
    35.         rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);
    36.  
    37.         if (Input.GetButtonDown("Jump") && IsGrounded())
    38.         {
    39.             //jumpSoundEffect.Play();
    40.             rb.velocity = new Vector2(rb.velocity.x, jumpForce);
    41.         }
    42.  
    43.         UpdateAnimationState();
    44.     }
    45.  
    46.     private void UpdateAnimationState()
    47.     {
    48.         MovementState state;
    49.  
    50.         if (dirX > 0f)
    51.         {
    52.             state = MovementState.walk;
    53.             sprite.flipX = true ;
    54.         }
    55.         else if (dirX < 0f)
    56.         {
    57.             state = MovementState.walk;
    58.             sprite.flipX = false ;
    59.         }
    60.         else
    61.         {
    62.             state = MovementState.idle;
    63.         }
    64.  
    65.         if (rb.velocity.y > .1f)
    66.         {
    67.             state = MovementState.jumping;
    68.         }
    69.         else if (rb.velocity.y < -.1f)
    70.         {
    71.             state = MovementState.falling;
    72.         }
    73.         if (dirX>0f && Input.GetButtonDown("Jump") && IsGrounded())
    74.         {
    75.             //jumpSoundEffect.Play();
    76.             state = MovementState.side_jump_right;
    77.            
    78.         }
    79.         else if (dirX<0f && Input.GetButtonDown("Jump") && IsGrounded())
    80.         {
    81.             //jumpSoundEffect.Play();
    82.             state = MovementState.side_jump_left;
    83.         }
    84.            
    85.  
    86.         anim.SetInteger("state", (int)state);
    87.     }
    88.  
    89.     private bool IsGrounded()
    90.     {
    91.         return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround); // new box colider for our player (position,size,rotation,vector, offset from our original box)
    92.     }
    93. }
     

    Attached Files:

    Last edited: Mar 26, 2023
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,738
    Your animation may be overwriting it. Fire up the Animator window by double-clicking on the controller and select the states you care about and make sure they do NOT have the Write Defaults box checked.

    Otherwise, keep debugging! We know flipX works... isolate, isolate, isolate.
     
  3. antonkk21

    antonkk21

    Joined:
    Mar 26, 2023
    Posts:
    2
    Write Defaults is unchecked, now after i perform a right side jump flipX stucks on true no matter if i press left its still playing walk right animation :( so now everything is working but the walk stuck on right animation. basically switched the side of the problem. flipX keeps stucking and not updating even tho condiotions are met (dirx and flipx are showing correct in the console)
     
    Last edited: Mar 26, 2023
  4. MajZee

    MajZee

    Joined:
    Oct 22, 2023
    Posts:
    1
    Like Kurt said, II found that only having the Idle/First animation Write Defaults box checked worked for me.
    All other animations untick the Write Defaults box.

    Also try restarting your engine, I was getting random null errors in my console that I now cant seem to replicate.

    Good luck.

    flipx1.PNG flipx2.PNG newflipx1.PNG newflipx2.PNG flipx neww.PNG