So i have this script on a trigger area that changes text on a canvas when the player walks through. I want it to be able to change the text to something else if they walk backwards through it. Can someone provide me with a script that does this?
While @tanoshimi 's answer would work, I would do it differently. Instead, set up two colliders, next to each other. One triggers first and the other one later depending on which direction the player crosses them. You can check which collider triggered first and do your logic accordingly.
In my opinion, it’s simpler, easier to understand, and it doesn’t depend on the direction the player is facing. (Tanoshimi’s answer would fail if the player walks through the trigger facing backwards)
In the OnTriggerEnter callback, test the value of the transform.forward vector of the player, which gives the direction in which they’re facing. You can then use Vector3.Dot to take the dot product with another vector to see how similar it is to, say, Vector3.right or Vector3.forward to decide which text to display.
having two colliders could be problematic. what if i enter and exit only the first one, and never walk through the second? how would i know which is first? another way is to use a collider enter/exit and compare to direction of the colliders forward. this way, walking backwards will trigger the collider in the correct direction.