This question was
closed Sep 15, 2015 at 05:32 PM by
Jujek for the following reason:
The question is answered, right answer was accepted
Checking if a point is withing any of the colliders
Greetings. I have to check if the given point is within any of the 2d box colliders attached to objects contained in an array.
So, in short, I do:
public GameObject[] turretAllowedZones;
public Vector2 targetPoint; //some Vector2 here, not important
void Start () {
turretAllowedZones = GameObject.FindGameObjectsWithTag ("turretAllowedZone");
}
So now basically I have an array that consists of all the empty objects that have "turretAllowedZone" tag. Those empties, shattered in many places on the map, have 2D Box Colliders attached.
I've been thinking about using "foreach" and the Bounds.Contains() function... So every objects gets checked one by one.
Any solutions please? Thank you in advance.
Comment
Best Answer
Answer by Jujek · Sep 15, 2015 at 05:32 PM
Okay I've solved it myself...
using UnityEngine;
using System.Collections;
public class turretBuilder : MonoBehaviour {
public GameObject testturret;
public GameObject[] turretAllowedZones;
void Start () {
turretAllowedZones = GameObject.FindGameObjectsWithTag ("turretAllowedZone");
}
void FixedUpdate () {
if (Input.GetKeyDown ("space")) {
// basic turret construction func | enter building mode
Vector3 cmouse = Camera.main.ScreenToWorldPoint (Input.mousePosition);
cmouse.x = Mathf.Round (cmouse.x);
cmouse.y = Mathf.Round (cmouse.y);
cmouse.z = 0.0F;
//print (cmouse);
foreach (GameObject turretAllowedZone in turretAllowedZones)
{
if (turretAllowedZone.collider2D.bounds.Contains (cmouse) == true) {
Instantiate (testturret, cmouse, Quaternion.identity);
}
}
}
}