Godot Version
4.2
Question
Hello, Again I am wondering what would be the best way to create animations for cycling on a bike, and fishing? Thanks
4.2
Hello, Again I am wondering what would be the best way to create animations for cycling on a bike, and fishing? Thanks
What have you tried so far? Have you made any animations before?
``This is my code so far, the animation works for running, walkin’ and idle using the animatedsprite2D so do I need to use the animation player for the rest of the animations because I want to add swimming as well?
extends CharacterBody2D
@onready var anim = $AnimatedSprite2D # Reference to the AnimatedSprite2D node
#Define movement speed variables
var movement_speed = 100
var run_speed = 600
var bike_spped = 800 # Speed when biking
#Variable to store direction
var dir = “idle” # Default direction
var playerState = “idle” # Player’s current state (idle or walking)
func update_player_state(direction: Vector2):
# Update player state based on direction
if direction.x == 0 and direction.y == 0:
playerState = “idle”
else:
playerState = “walking”
# Update direction based on movement
if direction.x < 0:
dir = “left”
elif direction.x > 0:
dir = “right”
elif direction.y < 0:
dir = “up”
elif direction.y > 0:
dir = “down”
# Debugging print to check player state and direction
#Handle input and update velocity
func handle_input():
# Get the movement direction based on input
var move_direction = Input.get_vector(“ui_left”, “ui_right”, “ui_up”, “ui_down”)
velocity = move_direction * movement_speed
# Check if the player is running
if Input.is_action_pressed(“run”): # Assume “run” is defined in the Input Map
velocity = move_direction * run_speed
# Update the player state and direction
update_player_state(move_direction)
# Determine the movement animation based on the direction
if move_direction != Vector2.ZERO:
# Debugging print to track the direction and current animation
# Corrected ternary operator
if dir == "right":
anim.play("Right_Run" if Input.is_action_pressed("run") else "Right_Walk")
elif dir == "left":
anim.play("Left_Run" if Input.is_action_pressed("run") else "Left_Walk")
elif dir == "up":
anim.play("Back_Run" if Input.is_action_pressed("run") else "Back_Walk")
elif dir == "down":
anim.play("Front_Run" if Input.is_action_pressed("run") else "Front_Walk")
else:
# Play idle animations based on last direction
if dir == "left":
anim.play("Left_Idle")
elif dir == "right":
anim.play("Right_Idle")
elif dir == "up":
anim.play("Back_Idle")
elif dir == "down":
anim.play("Front_Idle")
#Process physics each frame
func _physics_process(_delta):
# your code here
handle_input()
# Move and slide based on velocity
move_and_slide()
#This function is called when the animation changes (optional)
func _on_animated_sprite_2d_animation_changed():
# This can be useful for debugging to track which animation is currently playing
var _current_animation = anim.animation
Yeah, that sounds like a good approach.
You’re already making calls to anim.play() to play appropriate animations, so you should be able to add more branches to the logic there. For example you could modifiy this section a little to include more options:
anim.play("Right_Run" if Input.is_action_pressed("run") else "Right_Walk")
Something like this (changing the animation names and state names to match what you have set up):
anim.play(
"Right_Bike"
if playerState == "on_bike"
"Right_Swim"
if playerState == "swimming"
else "Right_Run"
if Input.is_action_pressed("run")
else "Right_Walk"
)
You will have to do something similar for each of the anim.play calls, but if you do it to just one of them at first you can check to see if it works.
If you want a less repetitive solution you could also set the up, down, left, right and idle part separately from the activity part, then combine them when you call anim_play(). Something like:
var anim_direction: String;
var anim_activity: String;
if dir == "right":
anim_direction = "Right";
elif dir == "left":
anim_direction = "Left";
elif dir == "up":
anim_direction = "Back";
elif dir == "down":
anim_direction = "Front";
if playerState == "swimming":
anim_activity = "Swim";
elif playerState == "biking":
anim_activity = "Bike";
elif move_direction.is_zero_approx():
anim_activity = "Idle";
elif Input.is_action_pressed("run"):
anim_activity = "Run";
else:
anim_activity = "Walk";
anim.play(anim_direction + "_" + anim_activity);
I hope that helps.