Hi all,
Looking for advice or a solution regarding the enemy trigger to flip the sprite when it reaches an intersection of the platform tilemap.
I can currently see two relevant scenarios:
- When the tilemap joins a wall and the enemy hits the vertical wall
- When the tilemap ends and the enemy reaches a “cliff”
With my current configuration scenario 1 is failing but scenario 2 is working.
For both scenarios, It appears that the OnTriggerEnter2D
is executed when the game starts and the BoxCollider2D
is already in contact with the Ground
layer and Platforms tilemap collider.
However, it seems that the tilemap collider is being seen as one continuous object and the OnTriggerExit2D
is not executed when passing a border, only when exiting the entire object (the part I refer to as “cliff”).
I have attached screenshots of the failing scenario to highlight.
a. Game start, enemy trigger executed (see console)
b. Enemy has passed vertical wall, no trigger (see console)
c. Enemy passes “cliff” trigger exit executed (see console). At this point enemy flips
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyMovement : MonoBehaviour
{
Rigidbody2D enemyRigidBody2d;
[SerializeField] float enemyMoveSpeed = 2.5f;
void Start()
{
enemyRigidBody2d = GetComponent<Rigidbody2D>();
}
void Update()
{
enemyRigidBody2d.velocity = new Vector2(enemyMoveSpeed, 0f);
}
private void OnTriggerEnter2D(Collider2D other) {
Debug.Log("Entering trigger");
}
private void OnTriggerExit2D(Collider2D other)
{
Debug.Log("Exiting trigger");
// flip sprite and turn around
enemyMoveSpeed = -enemyMoveSpeed;
transform.localScale = new Vector2(-(Mathf.Sign(enemyRigidBody2d.velocity.x)), 1);
}
}
Please can you advise?
Thanks!
Unity version: