Rotate the camera and the player (2D)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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.
enter image description here

I probably missed some vital detail, if I did please let me know. Thanks in advance! :slight_smile:

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.

dregent | 2021-09-03 20:57

:bust_in_silhouette: Reply From: exuin

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:

func _input(event: InputEvent) -> void:
mouseSpeed = Input.get_last_mouse_speed()
if mouseSpeed.x > 0:
	self.rotation += abs(mouseSpeed.x) / 15000
elif mouseSpeed.x < 0:
	self.rotation -= abs(mouseSpeed.x) / 15000

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. :slight_smile: And it’s not the fps, the walking is smooth.

dregent | 2021-09-04 08:59

:bust_in_silhouette: Reply From: dregent

I did it, so here’s the solution for someone googling this stuff.

  1. first I use the _input() built in function
  2. then I check whether the event is a mouse movement
  3. 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