Topic was automatically imported from the old Question2Answer platform.
Asked By
Onulator
I’ve been looking into this for about a week or two now, and I still have no idea what’s going wrong. Basically, I’ve set up the animation player with 3 animations to activate on certain button presses in the process function, but when opening the debugger, it doesn’t work. And yet if I set it to loop and have it start up immediately in the ready function, it works properly. No matter what I do, what tutorial I look up, anything, I can’t get it to work, even though I’m pretty sure I’m using the correct code.
Am I just missing something or what?
extends KinematicBody2D
onready var P1_Animations = $P1_Animations
var isInCombo = false
var timeTillNextInput = 0.5
var time = 0
var currentAttack = 0
var previousAttack = 0
#-------------------------------------------------------------------------------
func _ready(): #Loads on startup
time = timeTillNextInput
#-------------------------------------------------------------------------------
func _proccess(delta): #Game checks all of this every frame
if(Input.is_action_just_pressed("P1_Attack1")):
if(currentAttack == 0):
P1_Animations.play("N-Attack1-1")
elif (currentAttack == 1):
P1_Animations.play("N-Attack1-2")
isInCombo = true
currentAttack += 1
if(isInCombo):
time -= delta
if(time < 0):
time = timeTillNextInput
isInCombo = false
currentAttack = 0
P1_Animations.play("RESET")
if Input.is_action_pressed("ui_right"):
$P1_Animations.play("N-Attack1-1")
else:
$P1_Animations.stop()
#-------------------------------------------------------------------------------
You can’t force animationplayer to play in process(), because it starts animation from first frame once every process frame ! Your animationplayer works, but is frozen in frame 0. You need to design a way to activate play() once, just like ready() function is activated once
Any advice on how to do that? I’m very new to Godot and would need more experience to figure out how to do that on my own.
I think the problem is on your script attachment… looks like you attach the script to KinematicBody2D (beacause first line is extends KinematicBody2D)
so if the AnimationPlayer Node(which you name it P1_Animations) is not child node of KinematicBody2D then You can’t access to it by $P1_Animations and all of the P1_Animations.play('...') will not work.
So I was having a similar Issue: Im calling my animations with custom functions in player script that plays the animations but my animations are not playing properly while character is moving.
The Problem: When holding movement key set to process, process is constantly resetting the animation every frame…
Solution: Add a Timer that delays the Animation reset…
so Once the key is held it plays animation then add a timer that only allows it to reset every 2-5 seconds or so
Haven’t tried this yet but I believe that is the solution.
Ill let ya know how it goes
You can just add a state flag to see if the timer is running, if it is don’t initialize, if it’s not running, set state flag to running and then initialize the timer.
Hey,
I’m having a similar issue with my platformer game. I’m using Godot’s built-in movement script, and when I add a code for idle and jumping, it seems to work fine, but for running, which is moving left and right, it seems to be stuck on a specific frame,
Wondering if someone could help?
Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/2d/default_gravity”)
func _physics_process(delta):
if (velocity.x > 1 or velocity.x < -1):
sprite_2d.animation = “running”
# Add the gravity.
if not is_on_floor():
velocity.y += gravity * delta
sprite_2d.animation = “Jumping”
else:
sprite_2d.animation = “default”
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var direction = Input.get_axis("ui_left", "ui_right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, 10)
move_and_slide()
var isleft = velocity.x < 0
sprite_2d.flip_h = isleft