Help with look_at() function

Godot Version

4.1.1

Question

I have a Node2D called StickAbility which has the below code:

func _process(delta):
	look_at(get_global_mouse_position().rotated(PI/2))

This node is attached as a child to a CharacterBody2D called Player.

When my player walks forward (Vector2.UP) the Node2D that should look at the global mouse position moves. Even though I am not moving the mouse. I don’t really understand what is going on here.

Here is a gif which hopefully demonstrates the problem. You can see as soon as the player walks forward, the stick moves even though the mouse hasn’t.

Here is my Player code which StickAbility is attached to:

extends CharacterBody2D

@onready var velocity_component = $VelocityComponent as VelocityComponent
@onready var stick_ability = $StickAbility

var base_speed = 0

func _ready():
	base_speed = velocity_component.max_speed


func _process(delta):
	var movement_vector = get_movement_vector()
	var direction = movement_vector.normalized()
	velocity_component.accelerate_in_direction(direction)
	velocity_component.move(self)


func get_movement_vector():
	var x_movement = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
	var y_movement = Input.get_action_strength("move_down") - Input.get_action_strength("move_up")
	
	return Vector2(x_movement, y_movement)

Does this work for the Node2D?

func _process(delta):
	look_at(to_global(to_local(get_global_mouse_position()).rotated(PI/2)))

this look_at(get_global_mouse_position()) should already cover what you need no? because the node who has this line of code should already be facing where the mouse at

sword
i see no problem with the look_at function

i think your issues is sword’s image pivot

the code i use:

@export var sword:Sprite2D

func _process(delta):
	sword.look_at(get_global_mouse_position())
	sword.rotate(PI/2)

sword2

1 Like

this is old but, why the sword.rotate(PI/2)?

i do think it should add 90 degree instead of substracting 90

1 Like

Thank you for the answer, it makes more sense now

1 Like