Game Development Community

Player leaves trigger before staying

by M. Ugur Karakaplan · in Torque Game Builder · 07/14/2007 (3:29 pm) · 8 replies

Below is the simplified code of the trigger. When player hits the trigger, linear velocity is set to be 0. So the player does not stay in the trigger. In the debug screen what I get is "entered!-left!". I don't want this onleave callback to work before staying happens and I realized that problem is due to setting the linear velocity of the player to 0 in onenter callback, but I need that too. How can this be solved?

function trigger::onEnter(%this,%object)
{
   if(%object.class $= "player") {echo("entered!"); $plyr.setLinearVelocity(0,0);}
}

function trigger::onStay(%this,%object)
{
   if(%object.class $= "player") echo("stays *********************************");
}

function trigger::onleave(%this,%object)
{
    if(%object.class $= "player") {echo("left!");}     
}

#1
07/16/2007 (12:12 pm)
Do you need the linear velocity to be set to zero immediately? You could call a schedule in OnEnter so that the player has a bit of time to get onto the trigger instead of just touching it...?
#2
07/16/2007 (2:22 pm)
Actually what I want is, player touches the trigger and stops there, that is, onStay and onLeave don't work because onEnter stops the player getting in the trigger. But in a code similar to the above onLeave also works...

I want onEnter to stop the player so that onStay and onLeave will not work. How can that be done?
#3
07/16/2007 (2:30 pm)
Ah...so right now, the onStay callback isn't functioning, right? So it's just the onLeave that you need to stop from running?

If you don't want onStay and onLeave to work... in the onEnter collision callback, a simple way to do this would be to disable the onStay and onLeave callbacks for the trigger (or, to disable them and then schedule a function that would turn them back on after a moment). Or would this disrupt something?
#4
07/16/2007 (3:02 pm)
That kind of solved, but it may cause a problem in future. I really couldn't understand the logic behind having an onLeave callback without having an onStay callback. It is really weird. But anyway, your suggestion works for now. Thanks Melissa.
#5
07/16/2007 (3:05 pm)
Heh, yeah it's a bit weird. I think the onStay is only triggered when the object remains in the area for a while--probably at least one whole tick (which it isn't doing if it's just hitting the object and bouncing back out right away). But I'm not sure, will look into it...
#6
07/17/2007 (2:35 pm)
Another similar problem is that although I set the linear velocity of the player to 0 when there is a collision with the trigger, it is sometimes possible (without any special things really) to get in the trigger and stuck there. I put the code in onEnter callback of trigger but it still doesn't prevent the player to get in the trigger. I really don't know what to do...

//this is in player

function player::onCollision(%srcObj, %dstObj, %srcRef, %dstRef, %time, %normal, %contactCount, %contacts)
{
    %srcobj.setLinearVelocity(0,0);
    $img.setlinearvelocity(0,0);
}

//this is in trigger

function trigger::onEnter(%this,%object)
{
    $plyr.setLinearVelocity(0,0);
    $img.setlinearvelocity(0,0);
}
#7
07/17/2007 (3:07 pm)
I've had this problem as well--with tilemaps, objects, etc. It might be an unintended consequence of TGB having switched to tick physics and the order in which things are processed. What is your onCollision response (clamp? bounce? etc?)
#8
07/17/2007 (3:38 pm)
My collision physics is not active, so I don't use collision response (but it comes as clamp by default and I don't change it, but physics is off). What I need I think is a command like "move to the first contact point position if there is collision". But there is no such command I suppose and it is not possible to write a function for it... OR it is possible but I am lost in the documentation.