Goober's sprite flips as soon as it collides with Ginger(Player)

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);
}

}

I solved it all i needed was

if(other.tag == “Player”) {return;}
in OnTriggerExit2D

making sure that the enemy only turns if hits the wall.

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

Privacy & Terms