0 votes

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


extends Actor

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

func getinput():
velocity = Vector2(0,100)
if Input.is
velocity.x = SPEED
if Input.isactionpressed('moveleft'):
velocity.x = -SPEED
if Input.is
actionjustpressed('jump') and isonfloor():
velocity.y = JUMPFORCE

    velocity.y = velocity.y  + GRAVITY

func physicsprocess(delta):
velocity = move

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

Godot 3.2

asked May 28 in Engine by UniverseCreating575 (12 points)

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 isonfloor 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()

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

velocity = get_input(velocity)

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 "getinput". This should get the input and not "calculatenew_velocity". Hope you get what i mean.

Please log in or register to answer this question.

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.