class_name Zombie extends RigidBody enum Dir { none up down left right } var dir = Dir.none var lastdir var tlast = 0 var frame_i = 0 var images: Dictionary = {} # Called when the node enters the scene tree for the first time. func _ready(): pass # Replace with function body. func _physics_process(delta): var updown = ((Input.get_action_strength("p1_down") - Input.get_action_strength("p1_up")) * 2 * delta) var rightleft = ((Input.get_action_strength("p1_right") - Input.get_action_strength("p1_left")) * 2 * delta) if rightleft: if rightleft > 0: dir = Dir.right else: dir = Dir.left elif updown: if updown > 0: dir = Dir.up else: dir = Dir.down else: dir = Dir.none self.translate(Vector3(rightleft, 0, updown)) func getid() -> String: var id = "" match dir: Dir.none: id += "n" Dir.up: id += "f" Dir.down: id += "b" Dir.left: id += "l" Dir.right: id += "r" id += String(frame_i) return id func setimg(id: String): var mesh = get_node("MeshInstance") var material = mesh.get_surface_material(0).duplicate() material.albedo_texture = images[id] mesh.set_surface_material(0, material) # Called every frame. 'delta' is the elapsed time since the previous frame. func _process(delta): tlast += delta if tlast > 0.15: tlast = 0 frame_i += 1 if frame_i >= 8: frame_i = 0 if lastdir != dir: tlast = 0 frame_i = 0 lastdir = dir setimg(getid()) func _init(): for f in ["n", "f", "b", "l", "r"]: for i in range(8): var id = f + String(i) var path = "res://zombie/" + id + ".png" images[id] = load(path)