x


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? :)

more ▼

asked Jul 19 '12 at 02:14 PM

Sharpytwo gravatar image

Sharpytwo
18 1 2

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 :)

Jul 19 '12 at 05:36 PM Sharpytwo
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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);
}
more ▼

answered Jul 19 '12 at 06:51 PM

ScroodgeM gravatar image

ScroodgeM
7.6k 2 6

that worked, thanks :) kinda noobish error i guess, but a beginner has to learn! :)

Jul 20 '12 at 12:33 PM Sharpytwo

everybody was a beginner 8) it's OK 8)

Jul 20 '12 at 12:36 PM ScroodgeM

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

Jul 20 '12 at 12:57 PM Sharpytwo

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

Jul 20 '12 at 04:16 PM ScroodgeM

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.

Jul 20 '12 at 04:19 PM Sharpytwo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3316
x2482
x1362
x980
x335

asked: Jul 19 '12 at 02:14 PM

Seen: 414 times

Last Updated: Jul 20 '12 at 06:05 PM