Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Server |
I have been using GMS for a while, but am making the switch to Godot. I have been researching, but can’t find much, or am not understanding. If you have the time, please assist me with this, thank you.
player Script:
extends KinematicBody2D
var speed = Vector2(64,64)
var last_mouse_pos = null
func _input(event):
if event.is_action_pressed("game_move"):
last_mouse_pos = get_viewport().get_mouse_position()
func _physics_process(delta):
if last_mouse_pos:
var direction_vector = (last_mouse_pos - global_position)
if direction_vector.length() < 3:
return
var velocity = move_and_slide(direction_vector.normalized() * speed)
if velocity.x > velocity.y && velocity.x < -velocity.y:
$AnimationPlayer.play("up")
elif velocity.x < velocity.y && velocity.x > -velocity.y:
$AnimationPlayer.play("down")
elif velocity.x > 1:
$AnimationPlayer.play("right")
else:
$AnimationPlayer.play("left")
Thank you again for your assistance and knowledge.
- Server
Do you know how the code here works, and how it stops moving when below a certain distance? If so, it is only a minor change to call a method on the $AnimationPlayer
to play
an “idle” animation (or stop
).
spaceyjase | 2023-02-21 10:59