Detecting missing collision in 2D platformer to change direction for enemy - without Raycast
I can not use Raycast, because I have a WebGL Build, with which Raycasts don't work well. Now I want enemies to change their moving direction, when they reach the end of a platform.
How I did it with Raycast (the enemy has a Game Object on its front downside and when it doesn't have contact to a collider, it changes its direction):
if (!Physics2D.Raycast(down_Collision.position, Vector2.down, 0.1f))
{
moveLeft = !moveLeft;
Vector3 tempScale = transform.localScale;
if (moveLeft)
{
//SNAIL LOOKS TO THE LEFT
tempScale.x = Mathf.Abs(tempScale.x);
}
else
{
tempScale.x = -Mathf.Abs(tempScale.x);
}
//new sprite-orientation
transform.localScale = tempScale;
}
Now, since Raycast isn't working anymore, is there another easy to use option? Physics2D.OverlapCircle work fine for top collision, but I didn't found out a way to detect collision to the ground.
Answer by Anonymous620 · Jul 06, 2021 at 02:04 PM
I know this is old but you could try this: 1. Add an empty gameobject as a child of the player and position it where you had your RayCast. 2. Add a box collider to it 3. Make the collider a trigger 4. Add this script to the gameobject
Public Transform player;
void OnTriggerExit2D(){
//change direction
}
Your answer
Follow this Question
Related Questions
Player passenger moving when being pushed by two platforms 0 Answers
2D Platformer Character Acts Weird When Collide Side of a Platform 0 Answers
Compatible collision constraint methods 0 Answers
2D How do i choose one out of two or three colliding gameobjects of the same type? 2 Answers
How to move a game object to a position after selecting it 0 Answers