Making a boardgame, I want my player to trigger an event on the space they land on, but not trigger events as they move through other spaces. I’m trying to use OnTriggerStay, but my script seems to trigger from other spaces just from the piece moving through the field.
Should I be scripting in a completely different way and avoid using collision detection and triggers?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NodeEvent : MonoBehaviour
{
private void OnTriggerStay(Collider other)
{
Debug.Log(“You are now on this space”);
}
}
I don’t think you know what OnTriggerStay does, it fires as long as you touch a trigger. Reading the documentation usually gives you a good idea of what everything does. OnTriggerEnter has nothing to do with this.
If you are looking for a way to execute code only on a certain field you are approaching this the wrong way. There’s a million ways you could do this but i think one way you could do it without having to change everything would be this:
- Set each field trigger to inactive
- Determine the field that your player will land on
- Set the trigger of that field to active
- Execute code in OnTriggerEnter and deactivate the trigger.
- Repeat
basically: if you only have the trigger of the field that your player is going to land on active, that is the only place your code can execute and it will do what you want it to. I would advise you in general, though, to look at the documentation first and see what everything does.