This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Please help me with this code for only one jump in 3D...!

PeterPetersonPeterPeterson Posts: 2Member
edited December 2017 in Programming

With this code before I added the variable "canjump" I could make the main character (which in this case is a simple ball with RigidBody) jump, but it could jump infinite times before touching the floor again. So I hope someone can help me make it jump only one time after each time it touches the floor. The variable seems to be working when the scene starts, because the ball can jump one time, but after that, it can't jump anymore. I'm new to coding in GD, so please let me know any mistake in this code. For example, two of the things I don't know are whether the variables are in the correct lines and the correct code for collision with the floor which is a StaticBody.

  extends Spatial
#
  var canjump = true
#
  func _ready():
    set_process_input(true)
    pass
#
  func _input(event):
#
    if event.is_action_pressed("SPACE") and not event.is_echo() and canjump==true:
        self.get_node("BallRigidBody").apply_impulse(Vector3(0,0,0),Vector3(0,10,0))
        canjump=false
#
   func on_StaticBody_body_enter( body ):
    var name = body.get_name()
    if name == "FloorStaticBody":
        canjump=true

Answers

  • VegaKHVegaKH Posts: 8Member

    At just a quick glance, it looks like your only problem may be INDENTATION. Your if statement on line 11 is at equal indent to the clause below it. Try changing lines 9-13 to:
    func _input(event): if event.is_action_pressed("SPACE") and not event.is_echo() and canjump==true: self.get_node("BallRigidBody").apply_impulse(Vector3(0,0,0),Vector3(0,10,0)) canjump=false

  • PeterPetersonPeterPeterson Posts: 2Member
    edited December 2017

    I actually didn't know well how to make the text in the question be like a code, so I just pressed space randomly. I made some testing and my actual code in the project doesn't have this indentation problem. Sorry for the mistake in the question. I'd appreciate if you could still try to help me.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.