Why is my character not moving but the animation is going?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Andyroo

Im very new to programming thats been trying to create a 2D rpg game and ive been using tutorials to do specific tasks in godot and ive seem to run into a problem… I cannot move my 2d character but the animation is still going (on the spot) im not sure where and how to put the scripts, should i put them together or leave them seperate (animation and moving)

Animation script:
extends KinematicBody2D

#Called when the node enters the scene tree for the first time.
func _ready():
$AnimationPlayer.play(“walk_down”)

func _physics_process(delta):
if Input.is_action_pressed(“down”):
$AnimationPlayer.play(“walk_down”);
elif Input.is_action_pressed(“right”):
$AnimationPlayer.play(“walk_right”);
elif Input.is_action_pressed(“up”):
$AnimationPlayer.play(“walk_up”);
elif Input.is_action_pressed(“left”):
$AnimationPlayer.play(“walk_left”);

Movement script:

extends KinematicBody2D

var velocity : Vector2 = Vector2()
var direction : Vector2 = Vector2()

func read_input():
velocity = Vector2()

if Input.is_action_pressed("up"):
	velocity.y -= 1
	direction = Vector2(0,-1)
if Input.is_action_pressed("down"):
	velocity.y += 1 
	direction = Vector2(0,1)
	
if Input.is_action_pressed("right"):
	velocity.x += 1
	direction = Vector2(1,0
	)
if Input.is_action_pressed("left"):
	velocity.x -= 1 
	direction = Vector2(-1,0)
	
velocity = velocity.normalized()
velocity = move_and_slide(velocity * 200)

func _physics_process(delta):
read_input()

Ive attached the movement script to the KinematicNode2D in the player.tscn (player scene) and the animation script to the KinematicNode2D in the main.tscn (main scene)

:bust_in_silhouette: Reply From: Wakatta

Combining your scripts are a matter of personal preference. And if you do seperate your logic using functions as you have above.

extends KinematicBody2D

var velocity : Vector2 = Vector2()
var direction : Vector2 = Vector2()

func _ready():
    $AnimationPlayer.play("walkdown")

#Animation
func play_animation:
    if direction == Vector2.DOWN:
        $AnimationPlayer.play("walkdown")
    elif direction == Vector2.RIGHT:
        $AnimationPlayer.play("walkright")
    elif direction == Vector2.UP:
        $AnimationPlayer.play("walkup")
    elif direction == Vector2.LEFT:
        $AnimationPlayer.play("walkleft")

#Movement
func read_input():
    velocity = Vector2()

    if Input.is_action_pressed("up"):
        velocity.y -= 1
        direction = Vector2(0,-1)
    if Input.is_action_pressed("down"):
        velocity.y += 1 
        direction = Vector2(0,1)
    if Input.is_action_pressed("right"):
        velocity.x += 1
        direction = Vector2(1,0)
    if Input.is_action_pressed("left"):
        velocity.x -= 1 
        direction = Vector2(-1,0)

    velocity = velocity.normalized()
    velocity = move_and_slide(velocity * 200)

func _physics_process(delta):
    read_input()
    play_animation()

You can attach the scripts to separate nodes but it seems that you have the movement script placed on the incorrect node.

The animation script can be placed on the AnimationPlayer node and the movement script where the animation script is now