Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Tybobobo | |
Old Version | Published before Godot 3 was released. |
I am having an issue with the player not facing the mouse cursor when the camera is a child of the player (to move with it). This is the code I use to look towards cursor. It runs on the player, which is a RigidBody2D.
# Makes player face towards cursor
var mpos = get_viewport().get_mouse_pos()
self.look_at(mpos)
It is as if the player center is stuck on a certain position in the world; so that when I move the player away from where it was; the center of the player (where facing direction is being calculated) is stuck in space; regardless of camera movement. :S
The mouse coordinates on the screen does not move with the camera.
So far I have discovered that the on-screen mouse coordinates do not change when moving the camera. This mean I will need to add x and y from the player position. Are there any in-engine function to handle this for me? Else I will just have to do the math myself
Any tips or advice how I can get the camera and on-screen mouse coordinates to follow the camera?
Solution found:
At first I managed to solve it by using this line to get mouse coordinates;
var mpos = get_viewport().get_mouse_pos() + get_pos() - OS.get_window_size() / 2
… and then someone answered this question and it turned into
var mpos = get_global_mouse_pos()
I like the second one better