Godot Version 4.6
Hello, so I have a bit of an issue, I want to make a 2D rig in full lateral view, so I´m making some tests.
Right now I´m making a shooting character for practice, with no animations (yeah, I know I’m posting on the animation category, but bear with me) with an arm that follows the cursor, but the arm is not following the cursor correctly.
I´m using the look_at.(get_global_mouse_position()) for this.
extends CharacterBody2D
@onready var input_component = $InputComponent
@onready var movement = $Movement
@onready var sprite: Sprite2D = $body/torso
@onready var sprite_arm: Sprite2D = $body/arm
func _physics_process(delta:float) → void:
input_component._update()
movement.direction = input_component.move_dir
movement.tick(delta)
sprite_arm.look_at(get_global_mouse_position())
if get_global_mouse_position().x < global_position.x:
sprite.flip_h = false
else:
sprite.flip_h = true
Player
-CollisionShape2D
-body
-InputComponent
-Movement
That is how it looks, with the arm not looking at the cursor (is in y(0) x (16))
Also, I have already moved the pivot point towards the center of the arm.
A screen shot of the scene tree is much more informative than a text recreation. Even if, as a new user, you have to reply to your own post to post it.
Sure, sorry for the delayed response, I’ve been busy…
does the arm at least move when you move the mouse?
It does indeed, and I’ve already solved the issue.
The method look_at() rotates so that the +x axis point towards the defined vector, so it rotates based on that axis, in other words, if the sprite is not natively pointing in that direction, the rotation will not look as one wants it.
To correct this easily (although there are multiple methods) one can apply a rotation to the sprite inside the code, I used rotation of the sprite, like this:
extends CharacterBody2D
@onready var input_component = $InputComponent
@onready var movement = $Movement
@onready var sprite: Sprite2D = $body/torso
@onready var sprite_arm: Sprite2D = $body/torso/arm
func _physics_process(delta:float) -> void:
input_component._update()
movement.direction = input_component.move_dir
movement.tick(delta)
sprite_arm.look_at(get_global_mouse_position())
sprite_arm.rotation += deg_to_rad(270)
if get_global_mouse_position().x < global_position.x:
sprite.flip_h = false
else:
sprite.flip_h = true
And so, now the sprite is pointing accurately towards the mouse.
For other methods, I recommend this video: