Enemy Sprite Won't Flip?

So my enemy is moving gust fine but i was wondering how to make his sprite flip right when he goes right and flip left when he goes left, right now he just faces left and stays there, please help thanks!

using UnityEngine;
using System.Collections;


public class Walker : MonoBehaviour {


    public float walkSpeed = 2.0f;
    public float wallLeft = 0.0f;
    public float wallRight = 5.0f;
    float walkingDirection = 1.0f;
    Vector3 walkAmount;
    // Update is called once per frame
    void Update () {

        walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
        if (walkingDirection > 0.0f && transform.position.x >= wallRight)
            walkingDirection = -1.0f;
        else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
            walkingDirection = 1.0f;
        transform.Translate(walkAmount);
    }

}

When you inverse the walk direction also inverse the x value of transform.localScale

You may want to also add a check to ensure you don’t walk too far on the frame where you switch direction.

Instead of adjusting the moving direction you can rotate the object, this takes care of both where it’s going and where it’s facing. Depending on what you’re doing, inverting the scale can result in issues with child objects.

using UnityEngine;
using System.Collections;


public class Testing : MonoBehaviour
{


    public float walkSpeed = 2.0f;
    public float wallLeft = 0.0f;
    public float wallRight = 5.0f;
    Vector3 walkAmount;
    // Update is called once per frame
    void Update()
    {

        walkAmount.x = walkSpeed * Time.deltaTime;
        if (transform.position.x >= wallRight && transform.rotation.eulerAngles.y <= 0)
        {
            transform.Rotate(Vector3.up, 180);
        }
        else if (transform.position.x <= wallLeft && transform.rotation.eulerAngles.y >= 180)
        {
            transform.Rotate(Vector3.up, -180);
        }
          
        transform.Translate(walkAmount);
    }

}

The Vector3.Up specifies the y-Axis to rotate on.

he’s invisable and going backwards

he’s invisable when he goes right

See if this helps instead :smile:

using UnityEngine;
using System.Collections;

public class Walker : MonoBehaviour
{
    public float walkSpeed = 2.0f;
    public float wallLeft = 0.0f;
    public float wallRight = 5.0f;
    float walkingDirection = 1.0f;
    Vector3 walkAmount;
   
    // Update is called once per frame
    void Update()
    {
        walkAmount.x = walkingDirection * walkSpeed * Time.deltaTime;
        if (walkingDirection > 0.0f && transform.position.x >= wallRight)
        {
            walkingDirection = -1.0f;
            Vector3 flip = transform.localScale;
            flip.x *= -1;
            transform.localScale = flip;
        }
        else if (walkingDirection < 0.0f && transform.position.x <= wallLeft)
        {
            walkingDirection = 1.0f;
            Vector3 flip = transform.localScale;
            flip.x *= -1;
            transform.localScale = flip;
        }

        transform.Translate(walkAmount);
    }

}

When the enemy goes left he flips right but when he goes right he faces