Reputation: 85
I got an object named pacman, I want to know if there's another objects(coockies) at his right and left. For example, my pacman position is (-55,5,-55) and I want to know if there's a cookie at (-45,5,-55), 10 units at his right. I used Physics.OverlapSphere but it gave me all the cookies around pacman and I want to check separately right and then left.
Upvotes: 2
Views: 967
Reputation: 70
You should travel all Collider
from the return of Physics.OverlapSphere
, and check collider.transform.position
Upvotes: 1
Reputation: 967
Try this way.
public void Detect(GameObject pacman, float radius) {
Vector3 pacmanPosition = pacman.transform.position;
var hitColliders = Physics.OverlapSphere(pacmanPosition, radius);
for (var i = 0; i < hitColliders.Length; i++) {
if(hitColliders[i].gameObject.transform.position.x == (pacmanPosition.x-10)){
//TODO
}
else if(hitColliders[i].gameObject.transform.position.x == (pacmanPosition.x+10)){
//TODO
}
}
}
Upvotes: 0