Trouble changing to IDLE state
Asked Answered
C

6

0
point-click-copy.zip
3MB

I'm making a project where the player moves towards the position where you click and I want the player to change to the IDLE state when they reach that position, but I seem to be missing something when it comes to changing to the IDLE state.

Maybe someone could help me figure out what I'm doing wrong in my code?

Codd answered 3/3 at 17:54 Comment(0)
P
0

Codd could you post the code?

Pursy answered 4/3 at 0:56 Comment(0)
T
1

I added a print in Player.gd to investigate:

		States.WALK:
			print("WALK")
			print_debug(position - click_position) # debug
			if position.is_equal_approx(click_position): # doesn't work
				change_state(States.IDLE)
			else:
				speed = walk_speed

The output shows that position gets close to click_position, but not close enough for position.is_equal_approx(click_position) to be true.

It works better if I replace:
if position.is_equal_approx(click_position):
with:
if (position - click_position).length_squared() < 10.0:

But that's probably not the correct solution. You need to figure out why position doesn't get closer to click_position.

Also, when posting a project, you can omit the .godot folder, which is usually very big.

Taimi answered 4/3 at 1:56 Comment(0)
C
0

Taimi Thank you for the input. Sorry I didn't upload it right.

Codd answered 4/3 at 20:6 Comment(0)
C
0

Pursy

Codd answered 4/3 at 20:13 Comment(0)
C
0

Taimi I'll try to brainstorm this, but if anyone has any suggestions on how to remedy this, I'll appreciate it.

Codd answered 4/3 at 20:37 Comment(0)
P
0

Codd I think I found it. You are equating the position of your object with the global_position of the mouse. position is something's place relative to its parent and/or origin, while global_position is its place in the game world.

Go to line 36: (my abbreviations may be off)

STATES.run:
        PRINT("RUN")
        if global_position.is_equal_approx(click_position): ### this sees your actual place in the game world
                change_state(States.IDLE)
        else:
                speed = run_speed
~~~                
Pursy answered 9/3 at 23:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.