The player controls a physics ball and for traps I would like events to start when the ball in on a very specific place.
If I use Ontrigger, it will fire on touch, and adding a delay is no solution because the player might have changed his mind and move in the opposite direction, or have a to slow/high speed fo my trap timing is off and kills the player when he is not trapped at all.
So is there a way to check if the middle of the sphere collider is in the trigger before it fires,
or perhaps like halflife2 a trigger that fires when the player (collider) is completely entered the trigger volume?
Make smaller triggers and larger trap effect areas or you could always make a double trigger trap… have a small inner trigger that’s disabled and a larger outter trigger. The ontrigger for the outter enables the inner and the inner does whatever you want ensuring the player is inside the trap area.
function OnTriggerStay( other : Collider ) {
if ( /* other is the player */ ) {
if ( Vector3.distance( other.transform.position, transform.position ) < 1 ) {
Debug.Log("Triggered");
}
}
}
I want to go a bit further and check the distance between position.z,
not sure how to do this.
(thisPlayer.transform.position.z - transform.position.z) < 0.1
That only works when on the positive plane side.
Use the absolute value of the difference for the comparison, rather than the distance itself.
Also note that if you’re relying on the object position being within some distance of a specific point, if the object is moving fast enough relative to the time step, it may ‘tunnel through’ the trigger area. That may not be a problem in your particular case, but it’s something to be aware of.