- Home /
function OnTriggerEnter with two conditionals help
This doesn't work. Why?
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.name == "LadderTrigger" && Input.GetKeyDown("w"))
{
ScriptCamera.enableDynamicCam = true;
}
}
As BenProd notes, the thing the code actually checks for is probably not what you want.
$$anonymous$$ight help if you describe what you are trying to make happen (player presses a key while they are near a ladder?)
Answer by Benproductions1 · Jul 24, 2013 at 11:25 PM
Hello,
The reason this doesn't work, is because Input
is frame based. Meaning it works within the Update
functions etc...
But OnTriggerXXXX
is Physics based, so it uses FixedUpdate
, which is independant from the framerate.
There are plenty of examples of how to use Input
in FixedUpdate
. Use any of those and it should work :)
Another problem you will have, is that you are checking for 2 events, that only ever happen once. Only if you Enter the trigger AND press the button down at the exact same time, will anything happen. The chances anyone will be able to do this are very slim. I suggest either using Input.GetKey
or OnTriggerStay
Hope this helps,
Benproductions1
I second this, maybe you could put a flag = true during OnTriggerEnter, so that on your update you can check if the flag && Input.Get$$anonymous$$ey conditions are met. Then if OnTriggerExit you can set the flag off.
Ok I see what you mean. I also tried
if(other.name== "ladderTrigger")
{
if()Input.GetkeyDown("w"){
}
}
but I suppose that's the same thing.
That won't compile. The brackets need to be around the
Input
function.if (Input.Get$$anonymous$$eyDown("w"))
Yes it's exactly the same thing,
@Banholio That only works as long as you can only be in one trigger at once. If you can be in more than once, it will fail and bug out. Thats why I prefer OnTriggerStay
:)