- Home /
Ways to find bounds of the platform
Hello, in my 2D platformer enemies have simple AI: they walking from edge to edge of platform and chasing player if see him. They can't jump so if creep chased player and fell to another platform, he need to get the new bounds to walking.
I'm wandering: what is the best way to get this bounds?
I've tried to make a series of downwarding Raycasts (that hits only platforms) at the right and left of object and if i detect change of height, it would mean that we find edge of platform or obstacle.
For some reason my implementation of this algorithm doesn't work.
Here's my implementation:
LayerMask lm = (1 << LayerMask.NameToLayer("Ground")); // Layer with platforms
RaycastHit2D hit = Physics2D.Raycast(transform.position,(-Vector2.up),lm);
// Getting collision with the platform beneath our creep
float platformy = hit.point.y; // Saving its height
// Now finding right bound
int i = 1;
while(Mathf.Abs (hit.point.y-platformy)<0.1f)
{
hit = Physics2D.Raycast(new Vector2(transform.position.x+(i*0.1f),(transform.position.y)),(-Vector2.up),lm);
i++;
}
wallLeft = hit.point.x-0.1f;
// Left one
hit = Physics2D.Raycast(new Vector2(transform.position.x,(transform.position.y+2f)),(-Vector2.up),lm);
i = 1;
while(Mathf.Abs (hit.point.y-platformy)<0.5f)
{
hit = Physics2D.Raycast(new Vector2(transform.position.x-(i*0.1f),(transform.position.y)),(-Vector2.up),lm);
i++;
}
wallRight = hit.point.x+0.1f;
(Sorry for bad English)
Answer by mexey · Apr 06, 2014 at 12:39 PM
I finally realized what was happening. My algorithm works, but my mistake was that my layer mask was interpreted as distance parameter which i passed(because of this raycast was colliding with the creep). If someone interested, in the end my code looks like this and it works well (at least in my case, when gameworld is built from 0.7*0.7 blocks)
LayerMask lm = (1 << LayerMask.NameToLayer("Ground"));
RaycastHit2D hit = Physics2D.Raycast(new Vector2(transform.position.x,(transform.position.y+1.5f)),(-Vector2.up),Mathf.Infinity,lm);
float platformy = hit.point.y;
int i = 1;
while(Mathf.Abs (hit.point.y-platformy)<0.65f)
{
hit = Physics2D.Raycast(new Vector2(transform.position.x+(i*0.1f),(transform.position.y+1.5f)),(-Vector2.up),Mathf.Infinity,lm);
i++;
}
wallRight = hit.collider.transform.position.x - 0.8f;
hit = Physics2D.Raycast(new Vector2(transform.position.x,(transform.position.y+1.5f)),(-Vector2.up),Mathf.Infinity,lm);
i = 1;
while(Mathf.Abs (hit.point.y-platformy)<0.65f)
{
hit = Physics2D.Raycast(new Vector2(transform.position.x-(i*0.1f),(transform.position.y+1.5f)),(-Vector2.up),Mathf.Infinity,lm);
i++;
}
wallLeft = hit.collider.transform.position.x + 0.8f;
Your answer
Follow this Question
Related Questions
2D jump with Raycast 1 Answer
Platfomer 2d game block destroying raycasting nullreference exception problem 1 Answer
Question About Raycast and Sidescrollers 0 Answers
2D Platformer Ai Movement Decision Making Problems 0 Answers
How to determine if the player can jump, without using raycasts. (2D) 1 Answer