- Home /
simple overlap in 2d
I have a 2d game. The players control a gameobject with a boxcollider through key inputs. the enemies have a circle collider.
How do I check if the two colliders overlap (in c# pls)?
looked into Physics2D.OverlapCircleAll, which seems like a good bet - just cannot figure out how to use it - and all exsamples I have found so far only confused me more.
I only want the check to happen when user gives a key input, so I thought of doing something like the following might work;
` bool IsOverlap = False;
public void checkColisionForAttack(){
if (Input.GetKeyDown (KeyCode.F)) {
// check for overlap and if overlap set IsOverlap to true
if (IsOverlap){
}
}
}
Answer by komodor · May 14, 2015 at 01:56 PM
Collider2D[] colliders = Physics2D.OverlapCircleAll(centerPosition, radius);
if (colliders.Length > 0)
{
IsOverlap = true;
}
else
{
IsOverlap = false;
}
and colliders is array of those colliders inside circle so you can reach them
thanks :)
just a quick question. I get errors for centerPosition and radius - and from the other errors it seems as if centerPosition needs to be an vector2 while radius is a float. I take it I can decide radius as i see fit - but how do I know which vector2 to use(tried just writing vector2, but i still get errors)?
ins$$anonymous$$d of centerPosition put transform.position and it will put actual position of the object the component is on
ins$$anonymous$$d of radius put there distance you want to check (1f or 10f or 4.4564f or another float)
Your answer
Follow this Question
Related Questions
I need help with overlapping sprites and teleporting 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Sprite Sheet animation without fancy add-ons 1 Answer
Changing multiple toggle states. 0 Answers