I have a raycast on my character. When a specific trigger is in sight, I want to do something once. It is happening every frame. How do I limit the response to just happen once? Thanks.
if(Physics.Raycast(transform.position, Vector3.right, out hit))
{
if(hit.transform.name == "East")
Instantiate(prefab, new Vector3(0, 0, 9.75f), Quaternion.identity);
}
You can use a boolean variable to only instantiate the prefab when it is true and
be sure to make it false in the statement following the call. Something like this:
if(Physics.Raycast(transform.position, Vector3.right, out hit)) {
if(hit.transform.name == "East")
if(instantiatePrefab) {
InstantiatePrefab();
instantiatePrefab = false;
}
}
}
void InstantiatePrefab() {
Instantiate(prefab, new Vector3(0, 0, 9.75f), Quaternion.identity);
}
A slight modification to Ingot’s answer… for a little more efficiency you can move the bool check to the outermost scope so you don’t even perform the raycast unless you need to.