Hi! I'm new to godot and to programing, I wanted to make a little game (platformer).
I did the code for the character and it worked well, but then I added a script to run, and then the animation "Walk" stopped working.
This was the code before adding the script:
extends KinematicBody2D
const moveSpeed = 20
const maxSpeed = 40
const runSpeed= 25
const maxrunSpeed= 45
const jumpHeight = -115
const up = Vector2(0,-1)
const gravity = 10
onready var sprite = $Sprite
onready var animationPlayer = $AnimationPlayer
var motion = Vector2()
func _physics_process(delta):
motion.y += gravity
var friction = false
if Input.is_physical_key_pressed(KEY_D):
sprite.flip_h = true
animationPlayer.play("Walk")
motion.x = min(motion.x + moveSpeed,maxSpeed)
elif Input.is_physical_key_pressed(KEY_A):
sprite.flip_h = false
animationPlayer.play("Walk")
motion.x = max(motion.x - moveSpeed,-maxSpeed)
else:
animationPlayer.play("Idle")
friction = true
if is_on_floor():
if Input.is_physical_key_pressed(KEY_W):
motion.y = jumpHeight
if friction == true:
motion.x = lerp(motion.x ,0,0.5)
else:
if friction == true:
motion.x = lerp(motion.x,0,0.01)
motion = move_and_slide(motion,up)
This is the code after adding the script:
extends KinematicBody2D
const moveSpeed = 20
const maxSpeed = 40
const runSpeed= 25
const maxrunSpeed= 45
const jumpHeight = -115
const up = Vector2(0,-1)
const gravity = 10
onready var sprite = $Sprite
onready var animationPlayer = $AnimationPlayer
var motion = Vector2()
func _physics_process(delta):
motion.y += gravity
var friction = false
if Input.is_physical_key_pressed(KEY_D):
sprite.flip_h = true
animationPlayer.play("Walk")
motion.x = min(motion.x + moveSpeed,maxSpeed)
elif Input.is_physical_key_pressed(KEY_A):
sprite.flip_h = false
animationPlayer.play("Walk")
motion.x = max(motion.x - moveSpeed,-maxSpeed)
else:
animationPlayer.play("Idle")
friction = true
if is_on_floor():
if Input.is_physical_key_pressed(KEY_W):
motion.y = jumpHeight
if friction == true:
motion.x = lerp(motion.x ,0,0.5)
else:
if friction == true:
motion.x = lerp(motion.x,0,0.01)
motion = move_and_slide(motion,up)
if Input.is_physical_key_pressed(KEY_D) and Input.is_physical_key_pressed(KEY_CONTROL):
sprite.flip_h = true
animationPlayer.play("Walk")
motion.x = min(motion.x + runSpeed,maxrunSpeed)
elif Input.is_physical_key_pressed(KEY_A) and Input.is_physical_key_pressed(KEY_CONTROL):
sprite.flip_h = false
animationPlayer.play("Walk")
motion.x = max(motion.x - runSpeed,-maxrunSpeed)
else:
animationPlayer.play("Idle")
friction = true
if is_on_floor():
if Input.is_physical_key_pressed(KEY_W):
motion.y = jumpHeight
Basically, the "animationPlayer.play("Walk") in the first part, stopped working, but it works in the "run" script.