2D point-n-click animation help

Godot Version

4.6.stable

Question

Hi, brand new to Godot and trying to make a 2D point-n-click game, but I’m having trouble getting animations to play based the direction the character moves when the mouse is clicked. I’ve cobbled together my current script from a bunch of outdated tutorials and the Godot docs since I can’t find a whole lot on the specific kind of scripting I’m looking for, but I’ve gotten stuck.

I can get an Idle animation to play at the start of the debugger, but any walking animations triggered by clicking the mouse either won’t play or will only face one direction. I’ve tried flipping the sprite and playing a second walking animation but neither have worked. I want to keep the character restricted to movement along the x axis.

This is my current script:

extends CharacterBody2D


@export var speed = 200

@onready var animated_sprite = $AnimatedSprite2D

var click_position = Vector2()
var target_position = Vector2()
var direction = Vector2()

func _ready():
	click_position = position


func _physics_process(_delta):
	if Input.is_action_just_pressed("move"):
		click_position = get_global_mouse_position() 
	if direction.x == 0:
		animated_sprite.play("Idle01")
		
		
	if position.distance_to(click_position) > 10:
		target_position = (click_position - position).normalized()
		velocity = target_position * speed
		velocity.y = 0
		move_and_slide()
		
		
	if direction.x > 0:
		animated_sprite.play("WalkRight01")
	elif direction.x < 0:
		animated_sprite.play("WalkLeft01")

I should really try to reproduce this on a laptop and not from the top of my head on a phone, but here goes:

Have you tried using velocity.x in stead of direction.x down there?

And where are you assigning a direction to your direction variable? I think you are not. So it doesn’t do anything.

Try this:

func _physics_process(_delta):
	if Input.is_action_just_pressed("move"):
		click_position = get_global_mouse_position()
		
	if position.distance_to(click_position) > 10:
		target_position = (click_position - position).normalized()
		velocity = target_position * speed
		velocity.y = 0
		move_and_slide()
	else:
		velocity = Vector2.ZERO

	if is_zero_approx(velocity.x):
		animated_sprite.play("Idle01")
	elif velocity.x > 0:
		animated_sprite.play("WalkRight01")
	elif velocity.x < 0:
		animated_sprite.play("WalkLeft01")

Your character may jitter if its velocity makes it go past the target position and back all the time.. would have to play around with this magic number 10. Maybe export it as a variable.. :thinking:

We can do better than that.. (still editing)

And you should compare your character’s global position with the mouse cursor’s global position too.. this probably only works as long as you have a fixed camera..

I added a to_local around your get_global_mouse_position, but that may prove less wise than working with your character’s global_position in stead. (And got rid of that again due to your slingshot effect.. hopefully that fixes it)

I’m still working from my phone so if this has even no syntax errors I’d be pleasantly surprised.

And I think a better name for what you call target_position would be direction and a better name for what you’re calling click_position would be target_position.

And some of these variables could be declared inside your _physics_process … At least the one that gets a normalized direction (which you call target_position to your target position (which you call click_position).

I love point and click adventure games, by the way. Do you have a teaser screenshot?

Hi! that seems to have fixed the walking animation issue, thank you! This is my first attempt at making a game, so I appreciate the help! The idle animation doesn’t play when the character stops, but I will try and see if I can troubleshoot that myself. Thanks again! (:

Hi! I edited the answer some more. Might’ve even fixed your Idle state now too..

Make sure you do the official 2D tutorial!
It covers this stuff too.

Hi! unfortunately the edited script did not fix the idle animation and made the character slingshot way past the spot that was clicked, so I’ll keep the previous fix for now. I appreciate the help though!

Ahh . The slingshot.. that’s too bad. Now I left buggy code out on the forum… That’ll teach me to get rid of this phone forum addiction.