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.