#Making animation change when im walking

1 messages · Page 1 of 1 (latest)

carmine sun
#

Im fairly new and just want to make my 8 way movement script change the animation depending on direction and do a idle animation when im not moving. I already have all the animations setting up

extends CharacterBody2D

var SPEED = 100

func _physics_process(delta):

var input_vector = Vector2.ZERO

input_vector.x = Input.get_action_strength("Right") - Input.get_action_strength("Left")
input_vector.y = Input.get_action_strength("Down") - Input.get_action_strength("Up")

input_vector.normalized()


if input_vector:
    velocity = input_vector * SPEED
    move_and_slide()

I know this is probably terrible gd script but gddeadinside help me

manic ginkgo
#

Haha, I just had this problem and found the solution. Do you still need help?

carmine sun
#

YES PLEASE

manic ginkgo
#

sweet, so do you have any of the animations working yet or do you need help getting it started?

carmine sun
#

I have them working its just implementing them in the code

#

im kinda dumb with the code since im new

manic ginkgo
#

no worries, me too. First thing you gotta do is connect your Animated_Sprite_2D to your script as a variable. So go to the scene you're trying to get animated and drag the AnimatedSprite2D into your script while holding Ctrl it should paste something like
"@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D"
Let me know when you get that far

carmine sun
#

im using the normal sprite 2d with a animation node

manic ginkgo
#

you're not using a AnimatedSprite2D?

carmine sun
#

nope im using animation player

#

on a sprite 3d node

#

2d*

manic ginkgo
#

darn, not sure how to help, do you care to switch or are you set on using the animation that way?

#

Wait, so you're using a Animation player inside of a sprite2d inside of a characterbody2d? Is there a benefit to that preferred to just a charactedbody2d and animatedsprite2d?

carmine sun
#

I wanna do it this way since ive done it the other way and im tying to learn new techniques i think the code just needs to start playing a animation when im moving down for example im just not sure how to do that should i have it play the animation when im pressing the down key?

manic ginkgo
#

I just started and havent played around with the animationplayer yet, try just playing the animation in however you do that while the player isn't moving for your Idle animation.
So for idle animation try something like:
if direction == 0:
Animationplayer.play("Idle")

#

with my code, if direction is 0 that means the player isn't moving and I dont know what code plays the animation for the animationplayer so i just put animationplayer.play() as a placeholder

carmine sun
#

ill try that out

#

doing it rn

#

if SPEED == 0:
$AnimationPlayer.play("Idle")

#

im getting somewhere with this

#

it needs a little tinkering but you set me in the right direction thank yougdplushcheer

manic ginkgo
#

instead of checking if SPEED == 0, check if velocity == 0. I dont think SPEED is going to change

#

or after your code that checks
if input_vector:
velocity = input_vector * SPEED
move_and_slide()

add this afterwards:
if not input_vector:
PlayeryourIdleanimationhere

Also, i might be wrong, but I think you need to take move_and_slide() out of that if statement, just delete one indentation