I can't put the function ‘else : direction.x =0’ below ‘elif’ cause an error appears. And I jump infinitively.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Hakal87

extends KinematicBody2D

Player movement speed

export var speed = 75
const gravity = 10
const JUMP = -250
const FLOOR = Vector2(0, -1)

var direction: Vector2

func _physics_process(delta):

 if Input.get_action_strength("ui_right"):
  direction.x = speed
 elif Input.get_action_strength("ui_left"):
  direction.x = -speed
 if Input.get_action_strength("ui_up"):
	 direction.y = JUMP
	
 direction.y += gravity 
	
 direction = move_and_slide(d

It might help, if you provide the error message… Adding the else-case works perfectly fine for me. As you haven’t asked a real question, it’s hard to tell if you consider this a problem, but yes, using this script you can jump infinitely. There’s nothing in there to prevent that from happening. So I don’t understand what you expect.

njamster | 2020-04-27 12:13