Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Wolff |
This script is attached to a KinematicBody2D node. Trying to flip the Body sprite node relative to the mouse cursor’s position. Sprite should flip horizontally (face left) if the x position of the mouse is less than the x position of the player, but different errors say I’ve got it wrong (calling the wrong methods, apparently). Can anyone tell me what I’m missing or have incorrect? Thank you!
$Player
references the KinematicBody2D parent node, and $Body
references the Sprite node:
extends KinematicBody2D
export (int) var speed = 150
var velocity = Vector2()
func get_input():
velocity = Vector2()
if Input.is_action_pressed('ui_right'):
velocity.x += 1
if Input.is_action_pressed('ui_left'):
velocity.x -= 1
if Input.is_action_pressed('ui_down'):
velocity.y += 1
if Input.is_action_pressed('ui_up'):
velocity.y -= 1
velocity = velocity.normalized() * speed
func flip():
if get_global_mouse_position().x - $Player.global_position.x:
$Body.set_flip_h(true)
else:
$Body.set_flip_h(false)
func _physics_process(_delta):
get_input()
flip()
velocity = move_and_slide(velocity)