Hi, I've recently started a project in Godot, but I'm already stuck on something.
I'm making an isometric game where I want to move the player with the mouse, by clicking on a point and making the player move there. The movement works like i want it to, but the problem is that i don't know how to get the players angle to the mouse click position.
Here's the code: `extends KinematicBody2D
var moving = false var point = Vector2() var event = InputEvent() var direction = Vector2()
const speed = 200
onready var anim = self.get_node("AnimatedSprite") # animated sprite
enum ANIM_STATE { # animation states SOUTH, SOUTH_EAST, SOUTH_WEST, WEST, EAST, NORTH, NORTH_EAST, NORTH_WEST }
enum IDLE_STATE { I_SOUTH, I_SOUTH_EAST, I_SOUTH_WEST, I_WEST, I_EAST, I_NORTH, I_NORTH_EAST, I_NORTH_WEST } var State = null var Idle_State = null
var angle = null # player angle to mouse click
var walk_angle = angle
var idle_angle = walk_angle
func _ready():
set_fixed_process(true)
set_process_input(true)
func input(event): # if mouse btn 1 clicked, sets click position as vector2
if event.type == InputEvent.MOUSE_BUTTON:
if event.button_index == BUTTON_LEFT and event.pressed:
point = Vector2(event.x, event.y)
check_angle()
moving = true
func fixed_process(delta):
update_idle_sprite()
if moving: #moves player
var direction = point - self.get_global_pos()
update_walk_sprite()
if abs(direction.length()) < 5: #sets exact position of player
self.set_global_pos(point)
moving = false
return
direction = direction.normalized()
var motion = direction * speed * delta
motion = self.move(motion)
var slide_attempts = 4
while(is_colliding() and slide_attempts > 0):
motion = get_collision_normal().slide(motion)
motion = move(motion)
slide_attempts -= 1
func check_angle(): # supposed to check angle of player to mouse click point
var checkangle = round(self.get_global_pos().angle_to_point(point) * 5) /1
angle = checkangle
print(angle)
func update_walk_sprite(): # updates walk sprites based on angle value
if moving:
if walk_angle == 2 or 0 or 1 or -0 or -1 or -2 :
State = ANIM_STATE.NORTH
elif walk_angle == 3 or 4 or 5 or 6 :
State = ANIM_STATE.NORTH_WEST
elif walk_angle == 7 or 8 or 9 or 10 :
State = ANIM_STATE.WEST
elif walk_angle == 11 or 12 or 13 or 14 :
State = ANIM_STATE.SOUTH_WEST
elif walk_angle == 16 or -16 or -15 or 15:
State = ANIM_STATE.SOUTH
elif walk_angle == -3 or -4 or -5 or -6 :
State = ANIM_STATE.SOUTH_EAST
elif walk_angle == -7 or -8 or -9 or -10 :
State = ANIM_STATE.EAST
elif walk_angle == -11 or -12 or -13 or -14 :
State = ANIM_STATE.NORTH_EAST
if (State == ANIM_STATE.SOUTH):
anim.play("walking90")
elif (State == ANIM_STATE.SOUTH_EAST):
anim.play("walking135")
elif (State == ANIM_STATE.EAST):
anim.play("walking180")
elif (State == ANIM_STATE.NORTH_EAST):
anim.play("walking225")
elif (State == ANIM_STATE.NORTH):
anim.play("walking270")
elif (State == ANIM_STATE.SOUTH_WEST):
anim.play("walking45")
elif (State == ANIM_STATE.WEST):
anim.play("walking0")
elif (State == ANIM_STATE.NORTH_WEST):
anim.play("walking315")
func update_idle_sprite(): # updates idle sprite based on angle value
if not moving:
if angle == 2 or 0 or 1 or -0 or -1 or -2 :
Idle_State = IDLE_STATE.I_NORTH
elif angle == 3 or 4 or 5 or 6 :
Idle_State = IDLE_STATE.I_NORTH_WEST
elif angle == 7 or 8 or 9 or 10 :
Idle_State = IDLE_STATE.I_WEST
elif angle == 11 or 12 or 13 or 14 :
Idle_State = IDLE_STATE.I_SOUTH_WEST
elif angle == 16 or -16 or -15 or 15:
Idle_State = IDLE_STATE.I_SOUTH
elif angle == -3 or -4 or -5 or -6 :
Idle_State = IDLE_STATE.I_SOUTH_EAST
elif angle == -7 or -8 or -9 or -10 :
Idle_State = IDLE_STATE.I_EAST
elif angle == -11 or -12 or -13 or -14 :
Idle_State = IDLE_STATE.I_NORTH_EAST
if (State == IDLE_STATE.I_SOUTH):
anim.play("idle90")
elif (State == IDLE_STATE.I_SOUTH_EAST):
anim.play("idle135")
elif (State == IDLE_STATE.I_EAST):
anim.play("idle180")
elif (State == IDLE_STATE.I_NORTH_EAST):
anim.play("idle225")
elif (State == IDLE_STATE.I_NORTH):
anim.play("idle270")
elif (State == IDLE_STATE.I_SOUTH_WEST):
anim.play("idle45")
elif (State == IDLE_STATE.I_WEST):
anim.play("idle0")
elif (State == IDLE_STATE.I_NORTH_WEST):
anim.play("idle315")
`
When I print(angle) it show various numbers in the output feed (from -16 to -0 to the right of the player and from 16 to 0 to the left), but only the North walking and idle animations function properly, the rest don't play at all. I think the angle value is always at 0 despite what the output feed says, the player loads in as a static sprite facing south, but if i click anywhere, the animations for walking and idle( 0 angle value for both) are facing north, like I specified in the code .
I don't have much coding experience(at all) and I completely stumped. If anyone could help me, I'd very much appreciate it! Thank you in advance. :)