- Home /
Inconsistent Collision detection 2D on diagonal movement?
I'm writing a roguelike where all objects move in incriments of 1 square/tile whenever a button was pressed. Here is an example of enemy moving towards a player when player is to the down-left on diagonal:
if (target.transform.position.x < transform.position.x && target.transform.position.y < transform.position.y) //downleft
{
Collider2D obstacleDetection = Physics2D.OverlapCircle(checkers[5].transform.position, 0.1f, obstacle);
Collider2D playerDetection = Physics2D.OverlapCircle(checkers[5].transform.position, 0.1f, player);
if (obstacleDetection == null && playerDetection == null)
{
newpos = new Vector2(transform.position.x - moveIncrement, transform.position.y - moveIncrement);
Move();
}
else if (obstacleDetection != null)
{
}
else if (playerDetection != null)
{
Debug.Log("Want to attack player up");
Attack();
}
}
I'm using 8 coliders called checkers that check all the directions around an object when they are moving. I've also made it so they are active only during a respective move attempt. Normally there are 3 outcomes, Enemy moves toward player, Enemy stops because of obstacle, enemy Attacks because there is a player next to them. However, I have this nasty bug in which whenever player stands right next to an enemy, and moves on diagonal, the Enemy not only moves with the player but also attack them at the same time. But the weird thing is: let's say player stands to the left of the enemy, when they move down-left they get hit, however when they move up-left they don't get hit... I've checked it and for whatever the reason when player is right next to an enemy and moves away on diagonal two sensors activate but only one activates when they choose the other diagonal - and it is like that for every side.
At this point I'm lost at what's causing it. I probably should just use some kind of pathfinding package or use raycast but I feel, I'm so close to figuring it out.
Your answer
Follow this Question
Related Questions
Enemy detection while pathfinding through the map 1 Answer
What is wrong with my pathfinding script? 0 Answers
A* Pathfinding (from Aron Granberg) - how to find nearest target 0 Answers
Gizmos.DrawLine - thickens? 5 Answers
how do I add abilities(Scripts/GameObjects) to my path finding agent 1 Answer