0 votes

Alright, so my plan was to have my character model play a landing animation upon landing. I used a variable, "jumped", to make sure that the character touched the ground after a jump, so that it won't loop the animation whenever it's touching the ground.
if Input.is_action_pressed("jump"):
var _jumped = 1
if is_on_floor() and jumped == 1:
$AnimationPlayer.play("land")
var _jumped = 0
I already solved all the errors it gave me, and it says that nothing is wrong. However, the animation doesn't play when it lands. I've been puzzling over this for almost an hour.

I'm also new to GDScript, so please be nice.

Godot version 3.4.4
in Engine by (12 points)

2 Answers

0 votes

A few things jump out at me...

The likely culprit is that you're using two different variable names (jumped and _jumped). I assume those should be the same variable.

Also, I'm not sure where this code is running, but is_action_pressed() will fire in every frame where the key is pressed. If you only want this to fire once when the action is first pressed, you'd want to change that to is_action_just_pressed().

Finally, your indention is all wrong above, but I assume that's an artifact of how you pasted the code into the forum. To get it to display better, just cut/paste working code into your message, select it, and press the { } button in the forum editor's toolbar.

by (12,460 points)

The reason one variable is "jumped" and another "jumped" is because the Engine told me to do that in the debug menu. I assumed it would work; guess not.
"is
action_pressed()" is me being an idiot, I forgot that there was the two.
Finally, yes, the indentation is just a forum issue, everything's indented properly in the code.

0 votes

As mentioned earlier, your jumped variable has mismatched names. (The second one is named jumped which will misfire)
However i think this is a case of your code firing too quick. You have your jump code but then under that jump code you have the switch. That creates a dependency where you always call jump and probably fire both pieces of code at once because you can’t call the second expression without hitting jump. You likely need to separate them, perhaps with a signal? Put a custom signal and fire it in that is
onfloor() function (what is that and why is it true?) and then when the signal fires then run the second half of the code. You also may have some quirks in your logic with the ison_floor function() being true and the variable being true. If both aren’t true your event doesn’t happen.

My money is on that jump code though. Break it apart.

signal jump_end

func _ready():
      self.connect(“jump_end”, self, “on_jumpend”)

Then your code:

> If Input.is_action_just_pressed(“jump”):
>        var _jumped = 1

Then put the signal in the isonfloor() function:
I

   emit_signal(“jump_end”)

Then the signal function:

func on_jumpend():
       if _jumped == 1:
            $AnimationPlayer.play(‘land’)
            _jumped = 0
by (291 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.