I have this tram loaded with a collision mesh that fits the inside. The collision mesh has a script that checks for player collision with OnTriggerEnter
, and then sets the variable tramMove to true. This variable is taken in another piece of script attached to the parent tram itself, moving it forward. The result I was going for here is that when the player steps aboard the tram, it moves.
In an effort to make it so that when the player is not aboard the tram, it stops, I’ve also added an OnTriggerExit
component, which sets the tramMove variable to false again.
The problem I’m getting now seems to be that the player now needs to stand on the border of the trigger mesh to activate the tramMove variable, i.e with their foot in the door. The moment they step inside the tram, away from any mesh faces, tramMove is set to false again, as the stepping away from the mesh exterior is taken by Unity as an OnTriggerExit.
I need the script to work consistently, so that it depends on the player’s being inside the mesh altogether, rather than just touching its bounding faces. I’ve tried, for example, checking if the amount of times the player has exited the mesh is an even number, but this can be broken by stepping in and out of the tram door whilst still remaining on the tram. So I’m stuck.
(Tram trigger script)
static var tramMove : boolean = false;
var exitCounter : int = 0;
function OnTriggerEnter(trigger : Collider)
{
if(trigger.gameObject.name == "Player") // If the player activates the trigger.
{
tramMove = true;
}
}
(Tram move script)
var speed : float = 0.1;
function Update ()
{
if(TramMove.tramMove == true)
{
transform.Translate(Vector3(speed, 0, 0));
}
}