I was building my levels during the create more levels part of the course and when I began creating my second level, I duplicated the enemy and placed it throughout the scene. The problem is that it keeps flipping as if the enemy detects the player as a wall and as soon as the player dies it flips but continues to go straight.
Enemy Script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
[SerializeField] float moveSpeed = 1f;
Rigidbody2D myRigidbody;
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
}
void Update()
{
myRigidbody.velocity = new Vector2(moveSpeed, 0f);
}
void OnTriggerExit2D(Collider2D other)
{
moveSpeed = -moveSpeed;
FlipEnemyFacing();
}
void FlipEnemyFacing()
{
transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), 1f);
}
}