- Home /
Checking trigger collision on other objects?
Im currently working on a tower defence game , and in that game you are only allowd to build inside a area. So i need to check if the grid object is inside a trigger.
This is my current script.
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
public float gridSpacing = 5.0f;
public Transform grid;
public GameObject tower;
// Update is called once per frame
void Update () {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
bool rayhit;
rayhit = Physics.Raycast(ray.origin, ray.direction, out hit);
Vector3 position = hit.point;
position.x = Mathf.Round(position.x / gridSpacing) * gridSpacing;
position.y = Mathf.Round(position.y / gridSpacing) * gridSpacing;
position.z = Mathf.Round(position.z / gridSpacing) * gridSpacing;
grid.transform.position = position;
if (Input.GetButtonDown("Fire1")) {
Instantiate(tower, position, Quaternion.identity);
}
}
}
So i need to
if (Input.GetButtonDown("Fire1")) {
to also check if inside a trigger and the triggers tag.
Answer by rowdyp · Mar 21, 2011 at 06:18 PM
try telling it to print something like "Trigger Active" when initiate the trigger. That way you will know the trigger is active
Not the safest when using multiply triggers. I wanna have something that checks when u click to avoid bugs.
Answer by save · May 16, 2011 at 10:52 AM
I don't have any code but perhaps a method that could be helpful. You could store an array of available positions inside the grid, when a position is filled it cannot be built at. Just as an example, here position 1:3 and 2:2 is not available for building:
[ ][ ][x]
[ ][x][ ]
[ ][ ][ ]