Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | YeyoKermit |
Recently i´ve been coding a Player State A.I. to help reduce the effort when adding animations to my KinematicBody2D. Been checking the code and I don´t find any errors on it but everytime I run the main scene, KinematicBody.gd ( Player´s movement code ) is completely ignored resulting on my player not being able to do anything ( apart from animations ). Strangely my Input Events do work ( for example if I press up, jumping animation is displayed but action isn´t made (jump) . Parameters such as gravity are also not being applied.
Here´s my code for reference
**func _ready()**:
*add_state("idle")
add_state("run")
add_state("jump")
add_state("fall")
call_deferred("set_state", states.idle)*
**func _state_logic(delta):**
*parent._physics_process(delta)
parent._get_player_input(delta)
parent._input_jump()*
**func _get_transition(delta):**
*match state:
states.idle:
if !parent.is_on_floor():
if parent.motion.y < 0:
return states.jump
elif parent.motion.y > 0:
return states.fall
elif parent.motion.x != 0:
return states.run
states.run:
if !parent.is_on_floor():
if parent.motion.y < 0:
return states.jump
elif parent.motion.y > 0:
return states.fall
elif parent.motion.x == 0:
return states.idle
states.jump:
if parent.is_on_floor():
return states.idle
elif parent.motion.y >= 0:
return states.fall
states.fall:
if parent.is_on_floor():
return states.idle
elif parent.motion.y < 0:
return states.jump
return null*
**func _enter_state(new_state, old_state)**:
*match new_state:
states.idle:
$Sprite.play("Idle")
states.run:
$Sprite.play("Run")
states.jump:
$Sprite.play("Jump")
states.fall:
$Sprite.play("Fall")*
**func _exit_state(old_state, new_state):**
*pass*```
My node organization on the editor seems fine, no problems with it
[wrap=comment]
Well, If you could show the *Player Script* That would certanly help.
(Also as I see, we watched the same tutorial on statemachines XD)
[wrap=footnote]Czselu349 | 2020-06-30 19:24[/wrap]
[/wrap]
[wrap=comment]
Sure.
```extends **KinematicBody2D**
****c****onst TARGET_FPS = 60**
const ACC = 8
const MAX_SPEED = 64
const FRICTION = 12
const GRAVITY = 4
const JUMP_FORCE = 135
const AIR_RESISTANCE = 0.1**
**onready var animatedSprite = $Sprite
var motion = Vector2.ZERO**
**func _physics_process(delta):**
motion.y += GRAVITY * delta * TARGET_FPS
motion = move_and_slide(motion, Vector2.UP)
**func _get_player_input(delta):**
var x_input = Input.get_action_strength("ui_right") -Input.get_action_strength("ui_left")
if x_input != 0:
if is_on_floor():
motion.x += x_input * ACC * delta * TARGET_FPS
motion.x = clamp(motion.x, -MAX_SPEED, MAX_SPEED)
if is_on_floor():
if x_input == 0:
motion.x = lerp(motion.x, 0, FRICTION * delta)
**func _input_jump():**
if Input.is_action_just_pressed("ui_up"):
motion.y = -JUMP_FORCE
if Input.is_action_just_released("ui_up") and motion.y <-JUMP_FORCE/2:
motion.y = -JUMP_FORCE/2```
[wrap=footnote]YeyoKermit | 2020-06-30 19:35[/wrap]
[/wrap]