Topic was automatically imported from the old Question2Answer platform.
Asked By
dregent
First of all, hi!
I’m new to this engine, but have been using GMS for some time. I searched for a couple of days, and have not found my answer anywhere on the internet.
My goal is to rotate the camera with the player, who’s walking in a top-down-shooter style [eg. Robotron2084]. I want the player to appear stationary relative to camera.
So in a sense use look_at() to follow the mouse pointer. I can do it with buttons, like A and D to rotate a camera. But that’s not the point. I also tried using calculated angles to add or subtract to the player and camera rotation (based on get_last_mouse_speed < or > 0). But it was really glitchy, I must have been doing something wrong.
Here’s my scene hierarchy. The camera follows the player via the script.
Here’s my poor visual representation, I apologise for the bad photoshop. I hope it helps.
I probably missed some vital detail, if I did please let me know. Thanks in advance!
Is there any reason you can’t have the camera as the child of the player? It will be able to rotate automatically that way.
exuin | 2021-09-03 19:16
Unfortunately when I do that it constantly spins around. I use look_at(get_global_mouse_position()) on player script to rotate him.
I think the issue is the controls. Since you’re rotating the player to face the mouse pointer, the player will never stop rotating since the mouse pointer will always be the same location relative to the player. Instead, you should rotate the player based on the movement of the mouse.
Thanks for Your assistance exuin!
I tried something like this on the player, with the camera set as a child node:
However no matter how I tweak the movement number it’s divided by it’s jittery and lags. I don’t know how to describe it, but it feels as though the mouse sensor was dirty. Which it’s not. And it’s not the fps, the walking is smooth.
I did it, so here’s the solution for someone googling this stuff.
first I use the _input() built in function
then I check whether the event is a mouse movement
and finally I rotate the player using “relative” property of the "InputEventMouseMotion class and multiply it by some floating point number to get a less sensitive controls
func _input(event: InputEvent) -> void: if event is InputEventMouseMotion: self.rotation += deg2rad(event.relative.x) * mouseSensitivity