I want to define when the player touches the ground, some things happen, but it always identifies that it doesn’t come into contact with the ground.
My version of Godot is 4.2.2.
This is my code:
extends CharacterBody2D
var dash = 300.0
var speed = 150.0
var jumpPower = -350.0
var jumpMax = 2
var jumpCount = 0
func _process(delta):
moviment(delta)
attack()
print(is_on_floor())
move_and_slide()
var gravity = ProjectSettings.get_setting("physics/2d/default_gravity")
func _physics_process(delta):
move_and_collide(Vector2(0, 1))
velocity.y += gravity * delta
var motion = velocity * delta
move_and_collide(motion)
func moviment(delta)
var direction = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
if direction != 0:
$Anim.play("Walk")
if direction > 0:
$Anim.flip_h = false
velocity.x += speed * delta
velocity.x = clamp(speed, 100.0, speed)
elif direction < 0:
$Anim.flip_h = true
velocity.x -= speed * delta
velocity.x = clamp(-speed, 100.0, -speed)
else:
velocity.x = 0
$Anim.play("Idle")
jump()
func jump():
if is_on_floor() and jumpCount != 0:
jumpCount = 0
if jumpCount<jumpMax:
if Input.is_action_just_pressed("move_up"):
jumpCount += 1
$Anim.play("Jump")
velocity.y = jumpPower
New contributor
TomicMaster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.