Sprite won't flip



    bool isMoving;

    Vector3 characterScale;
    float characterScaleX;

    // Start is called before the first frame update
    void Start()
    {
        
        isMoving = true;
        
        characterScale = transform.localScale;
        characterScaleX = characterScale.x;
        
    }

    // Update is called once per frame
    void Update()
    {

        if (transform.localPosition.x < waypoints[index].position.x && isMoving == true)
        {
            myAnimator.SetBool("Walking", true);

            transform.position = Vector2.MoveTowards(transform.position,
            waypoints[index].position,
            moveSpeed * Time.deltaTime);

        }

        else if (transform.localPosition.x > waypoints[index].position.x && isMoving == true)
        {

            characterScaleX = -characterScaleX;
            myAnimator.SetBool("Walking", true);
            
            transform.position = Vector2.MoveTowards(transform.position,
            waypoints[index].position,
            moveSpeed * Time.deltaTime);
        }

I’m trying to flip my sprite in 2D when moving towards different waypoints. I’ve tried to flip the localScale by adding a minus. I can see during runtime using Debug.log that the scale is constantly flipping between 1 and -1 but in the inspector and visually there’s no change at all.

Any ideas where I’m going wrong?

Thanks

Hi Whirz,

Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?

Maybe the issue is this line: characterScaleX = -characterScaleX;
If the condition is true, the value constantly “flips”. -(-1) becomes 1, and -(1) becomes 1.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.