Godot Version
4.22
Question
Hello.
I am currently trying to program tank animations.
I have the tank treads as a separate sprite from the tank itself.
Each tread is the same sprite, flipped across the y axis.
The forward and backwards animations work as intended but the additional animations do not.
There is an animation for clockwise, counterclockwise, left u-turn, and right u-turn movement.
Clockwise and counterclockwise are for when the player presses only the left or right movement inputs to rotate the tank.
Uturns are for when the player moves forwards or backwards while rotating.
The animations play in the editor, but not while debugging.
Notably, while tapping the left and right inputs while moving forwards or backwards, it will move forward a single frame of the animation, only resetting when I stop moving forwards or backwards.
Here is the code I am working with right now:
extends CharacterBody2D
const SPEED = 64.0
const TURN_SPEED = 1
const ROTATION_SPEED = 20
@export var weapon: Node2D
@onready var body_sprite := $tank_body_sprite
@onready var animation_player = $AnimationPlayer
@onready var collision_shape := $tank_collision_shape
var direction: Vector2 = Vector2.RIGHT
func _physics_process(delta):
var input_direction := Input.get_vector("cclockwise","clockwise","backward","forward")
if input_direction.x != 0:
#rotate direction based on input vector and apply turn speed, animate.
direction = direction.rotated(input_direction.x * (PI / 2) * TURN_SPEED * delta)
rotation = direction.angle()
if input_direction.x >=0:
animation_player.play('cclockwises')
if input_direction.x <=0:
animation_player.play('clockwises')
if input_direction.y != 0:
#forewards and backwards movement and animations.
if input_direction.y <=0:
animation_player.play('forwards')
if input_direction.y >=0:
animation_player.play('backwards')
velocity = lerp(velocity, (direction.normalized() * input_direction.y) * SPEED, SPEED * delta)
if input_direction.x != 0 and input_direction.y != 0:
#rotating and moving simultaneously, play animations
if (input_direction.y <=0 and input_direction.x <=0) or (input_direction.y >=0 and input_direction.x >=0):
animation_player.play('uturnright')
if (input_direction.y >=0 and input_direction.x <=0) or (input_direction.y <=0 and input_direction.x >=0):
animation_player.play('uturnleft')
else:
animation_player.play('idles')
velocity = Vector2.ZERO
#apply movement and velocity
move_and_slide()
I believe it has something to do with input_direction.x and how it is being used for rotation instead of velocity, but if I understand correctly, Input.get_vector should be a Boolean that detects if the respective input has been pressed and assigning a 1/-1 or 0 respectively to their assigned axis.
It should still be able to detect whether input_direction.x or .y <=0 or >=0 and play the animations like it does with the forwards and backwards animations.
I have tried doing Input.is_action_pressed(“”) if/ands to get this desired result, but it still has the same issue. Only forwards and backwards animations play.
Thank you in advance for your time.