- Home /
My movement function with jump won't work with collider function
this is for a sidescrolling platformer, I have a ground (a box) and sticking out on top of this is a trigger. the character is a rigidbody cube with applying forces for movement. whenever the character enters the trigger (whenever it touches the ground) i want the character to be able to jump, meaning that when the character is in the air i want to disable the jump function. i got the trigger working and i got a variable saying if it was grounded or not, but when i try to use that variable in the jump part the it seems like that part of the script stops working. so it doesnt react.
var jumpForce = 200; private var onGround = false;
function FixedUpdate() { if(Input.GetKey(KeyCode.UpArrow) && onGround) { rigidbody.AddForce(0,jumpForce * 1,0); } }
function OnTriggerEnter(character : Collider) { var onGround = true; print(onGround); } function OnTriggerExit(character : Collider) { var onGround = false; print (onGround); }
This is the part of the code that SHOULD handle jumping... I cannot for the life of me find a solution to this... i have looked at every forumpost and question i can find... it's probably a simple solution but I'm very fresh to programming so some help please? :)
well, it works now, no idea how it was fixed, but if someone comes upon the same problem just ask and ill post the code :)
Answer by ScroodgeM · Jul 19, 2012 at 06:51 PM
may be you should not declare new values inside methods? e.g. remove 'var' before 'onGround'?
function OnTriggerEnter(character : Collider) { onGround = true; print(onGround); } function OnTriggerExit(character : Collider) { onGround = false; print (onGround); }
that worked, thanks :) kinda noobish error i guess, but a beginner has to learn! :)
btw, i have another small problem with the colliders... how do i script a difference between them? because as it is now all triggers trigger the same event, i tried scripting a finish line (a separate trigger with a separate script, that only contained a OnTriggerEnter and an Application.LoadLevel but it still triggered all the other stuff, and the script for that one didnt even run, strange enough. im guessing it has to do with layers or something in that area, but i cant find any documentation handling several triggers. thanks for your help btw :) edit: basically i need help to make trigger A trigger event X, whilst trigger B triggers event Y
one of solutions is to use tags - you can tag an object that must raise the event and check for this tag in OnTriggerEnter method
yeah, but will that work for instance if the player enters one trigger, he can jump, and if he enters another he can open a door? because the only thing i managed to do with this was filter what colliders would trigger, not what each trigger did.
Your answer
Follow this Question
Related Questions
Trigger respawn after collision? 3 Answers
how to make triger fireball not to pass through spesific objects 2 Answers
Multiple Colliders malfunctioning 0 Answers
How having correct bounce on a rotating pinwheel(it's more complicated)? 0 Answers
How can I make a physics object jump a given height on collision regardless of current velocity? 1 Answer