also animation code that i am kinda proud of:
create a new script for the sprite and add this to it:
extends Sprite2D
var frame_num = 0
var framei = 0
var anime_time = 0
var backwards = false #for 3 frames of animation on the sprite sheet
func walk():
anime_time += 1
if anime_time % 15 == 0:
frame_coords.x = framei
frame_num += 1
if backwards:
framei -= 1
else:
framei += 1
if frame_num > 3:
frame_num = 0
framei = 0
anime_time = 0
backwards = false
func run():
anime_time += 1
if anime_time == 1:
framei = 4
if anime_time % 8 == 0:
frame_coords.x = framei
frame_num += 1
if backwards:
framei -= 1
else:
framei += 1
if frame_num > 5:
frame_num = 0
framei = 4
anime_time = 0
backwards = false
2. add a script for the player and add this to it:
extends CharacterBody2D
const SPEED = 300.0
var dir = 0
func _physics_process(delta):
if !Global.player_can_move:
$Main.frame_coords.x = 1
return "my mom has been deployed"
var input = Vector2(Input.get_axis("ui_left","ui_right"),Input.get_axis("ui_up","ui_down")).normalized()
var st = 0
if input:
velocity = input*SPEED*((float(Input.is_action_pressed("ui_cancel"))/2))
match Input.is_action_pressed("ui_cancel"):
false:
$Main.walk()
true:
if Input.is_action_just_pressed("ui_cancel"):
$Main.anime_time = 0
$Main.run()
dir = check_direction(input)
$Main.frame_coords.y = dir
else:
velocity= lerp(velocity,Vector2.ZERO,0.6)
$Main.frame_coords.x = 1
move_and_slide()
func check_direction(input:Vector2):
if input.x > 0:
input.x = 1
elif input.x < 0:
input.x = -1
if input.y > 0:
input.y = 1
elif input.y < 0:
input.y = -1
print(input)
if input == Vector2(0,1):
return 0
if input == Vector2(-1,0):
return 1
if input == Vector2(1,0):
return 2
if input == Vector2(0,-1):
return 3
if input == Vector2(-1,1):
return 4
if input == Vector2(1,1):
return 5
if input == Vector2(-1,-1):
return 6
if input == Vector2(1,-1):
return 7
($main is the sprite node)
there might be some errors here based on your sprite sheet but if you are using a sprite with 4 or 8 directional movement that has each direction under the other then it should work fine
also the run funcition is optional if you have a run animation but if you don't you can just remove it and just increase the walk animation speed by 60%
3 comments