4

I have a character that a Boolean flag which determines whether or not he is jumping (if he is jumping, he cannot jump again). When he jumps, the jump flag is set to true, and when he collides with something it is set back to false (so he can jump again).

However, he only jumps once, and jump stays true even when it's supposed to reset to false. Why is this so? My definition for OnCollisionEnter() is supposed to reset the flag.

var trump;
var jump = false;

function Start() {
// Assigns the Rigidbody to a variable
trump = GetComponent(Rigidbody2D);
// Variable Switches:

}

function OnCollisionEnter() {
    jump = false;
    trump.velocity.y = 0;
    trump.velocity.x = 0;
}

function FixedUpdate() {
    trump.velocity.x = Input.GetAxis("Horizontal") * 10;
    if ((Input.GetKeyDown(KeyCode.UpArrow)) && (jump == false)) {
        trump.AddForce(Vector2(0, 10), ForceMode2D.Impulse);
        jump = true;
    }
}

EDIT: Based on an answer given I tried adding the Collision parameter to OnCollisionEnter(), but it still doesn't look like the function is being called after I added a Debug.Log() inside of it to check. Is there something else wrong?

function OnCollisionEnter(collision: Collision) {
    jump = false;
    Debug.log("He Hit The Wall");
    trump.velocity.y = 0;
    trump.velocity.x = 0;
}

enter image description here

Serlite
  • 12,130
  • 5
  • 38
  • 49
Number1son100
  • 219
  • 2
  • 12
  • 3
    It's Trump. He doesn't let other people tell him what to do :-p - Seriously, though, `Why won't Trump jump more than once?` is by far the funniest and craziest question title I've seen in a while ;-) – John Slegers Mar 09 '16 at 17:28
  • This one caught my eye for that reason. XP But I'd really like to edit the title to be a more useful/searchable question for the future... – Serlite Mar 09 '16 at 17:31
  • @serlite - always just click edit anytime you see an edit to make – Fattie Mar 09 '16 at 17:47
  • @Number1son100 - you *can not* use "unityscript". It is deprecated, does not work, and is being removed from Unity. Fortunately c# is far easier. – Fattie Mar 09 '16 at 17:48

1 Answers1

4

Your function signature for OnCollisionEnter() is incorrect. When defining these MonoBehaviour-specific functions, it is imperative that the name and parameters of the function match that shown in the documentation. Otherwise, the MonoBehaviour won't recognize them and the function won't be called when expected.

In this case, OnCollisionEnter() requires a Collision parameter:

function OnCollisionEnter(collision: Collision) {
    jump = false;
    trump.velocity.y = 0;
    trump.velocity.x = 0;
}

Note: The above was written before the question was edited to implement the suggestion - before, OnCollisionEnter() was being defined without a parameter.

However, in your case that isn't the only problem - you're using Unity's 2D physics, which means you should actually be using the 2D variant of the function: OnCollisionEnter2D, which also takes a slightly different parameter of a Collision2D.

So you would need:

function OnCollisionEnter2D(collision: Collision2D) {
    jump = false;
    trump.velocity.y = 0;
    trump.velocity.x = 0;
}

Hope this helps! Let me know if you have any questions.

Serlite
  • 12,130
  • 5
  • 38
  • 49
  • How do I fix that? – Number1son100 Mar 10 '16 at 14:55
  • @Number1son100 Hey, I took a closer look at your problem and you're right, there was something else wrong. I have updated my answer to reflect this, let me know how it goes. – Serlite Mar 10 '16 at 15:12
  • Do you want double jump? – Barış Çırıka Mar 10 '16 at 15:13
  • Thank you! Though how would I do that? – Number1son100 Mar 10 '16 at 21:24
  • @Number1son100 Glad I could help out! I'll try to answer for Barış - in the case of double jump, you'd just have to change your logic somewhat to use a counter (like `var jumpsRemaining = 2`) for tracking the jumping, rather than a Boolean flag. Only allow jumping when `jumpsRemaining > 1`, and with every subsequent jump, subtract 1 from it. Then, reset it to the desired number of jumps (like 2) when you collide with something. – Serlite Mar 11 '16 at 16:06