I’m a beginner, in case that doesn’t become obvious very shortly.
I have a cube that I am moving around a plane with my mouse. It has a cube trigger collider and a rigidbody component attached. I have static trees that I downloaded from the Asset Store to which I added another cube trigger collider to. In my cubes script, I have both the onEnter and onExit trigger functions. They check if the other object is tagged as “Obstruction” which only the trees are, and if so they Debug.Log() “enter” and “exit”, respectively.
The issue is that once I move the cube into contact with one tree, my console fills up with:
“enter”
“exit”
“enter”
“exit”
“enter”
“exit”
etc…
I have the Cube’s script below, in full.
using System.Collections;
public class BuildingController : MonoBehaviour {
public float range = 100f;
Ray shootRay;
RaycastHit shootHit;
int terrainMask;
int collisionCount;
// Use this for initialization
void Start () {
terrainMask = LayerMask.GetMask ("Terrain");
collisionCount = 0;
}
// Update is called once per frame
void Update () {
shootRay = Camera.main.ScreenPointToRay (Input.mousePosition);
if(Physics.Raycast (shootRay, out shootHit, range, terrainMask))
{
transform.position = shootHit.point;
}
else
{
Debug.Log ("RayMiss");
}
if (Input.GetButton ("Fire1")) {
Destroy (gameObject);
//Place finalObject
}
else if (Input.GetButton ("Fire2")) {
Destroy (gameObject);
}
//Debug.Log (collisionCount);
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Obstruction") {
collisionCount++;
Debug.Log ("enter");
}
}
void OnTriggerExit(Collider other) {
if (other.tag == "Obstruction") {
collisionCount--;
Debug.Log ("exit");
}
}
}