I have problems when I put is_on_floor

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

I have problems when i put is_on_floor, without is_on_floor my character jumps, but when I put it, my character stop jumping, can someone help me?

|||||||||SCRIPT|||||||||

extends Actor

const JUMPFORCE = -1000
const SPEED = 300
const GRAVITY = 30

func get_input():
velocity = Vector2(0,100)
if Input.is_action_pressed(‘move_right’):
velocity.x = SPEED
if Input.is_action_pressed(‘move_left’):
velocity.x = -SPEED
if Input.is_action_just_pressed(‘jump’) and is_on_floor():
velocity.y = JUMPFORCE

	velocity.y = velocity.y  + GRAVITY

func _physics_process(delta):
get_input()
velocity = move_and_slide(velocity,Vector2.UP)

velocity.x = lerp(velocity.x,0,0.2)

|||||||||||||||||ENGINE|||||||||||||||||
Godot 3.2

What is Actor? Maybe this should be KinematicBody2D? (just guessing)

the rest looks good to me…
why are you doing:

func getinput():
    velocity = Vector2(0, 100) # <------

?

Are you sure is_on_floor is ever true? (just add print(is_on_floor()) somewhere, and see the output)

2 Recommandations:
.

  1. you are defining you constants as integers. Then you use them to calculate velocity which is a Vector2 (2 floats). In your case, this works perfectly fine, but can lead to horrifix bugs, that are hard to find.
    Try this, and you’ll understand:
print(1 / 10) # Output: 0
print(1.0 / 10.0) # Output: 0.1

.
2. You should not change velocity in you get_input().
It’s work and is fine, if you have very simple scripts, but makes debugging harder as it should be.
It’s better to calculate a relative Vector2 to your actual velocity, return it, and set/add/wahtever it to the actual velocity:

velocity += get_input()

OR
give velocity as an argument, calculate stuff, return it, and set it to the new velocity:

velocity = get_input(velocity)

why?
if you change variables in functions, it becomes very fast very unclear what happens to variables and there values where and when.
If you use the return, it’s very clear what happens to the velocity at that point.
In this case it’s extra missleading, because the function is “get_input”. This should get the input and not “calculate_new_velocity”. Hope you get what i mean.

whiteshampoo | 2020-05-29 07:23