How to make first person camera in _physics_process(delta)?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 20xxdd20
func _input(event):
if event is InputEventMouseMotion:
	# Rotates the view vertically
	$Head.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1))
	$Head.rotation_degrees.x = clamp($Head.rotation_degrees.x, -75, 75)
	# Rotates the view horizontally
	self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1))

I have this code to look around in 3D but I want it in _physics_process(delta) because probably _input(event) causes the jitter in my project.

You don’t want this code in _physics_process, 20xxdd20.
Doing this will cause the player to feel lag in their mouse input, which I think is more unacceptable than jitter. In the first place, this is not even possible in _process without convoluted code (which would definitely make the problem far worse).

But I’d be surprised if _input(ev) causes any sort of performance overhead for your project. Even if you do nothing and have no code at all, the engine ensures the following queries always takes place every single frame:

(has something happened? i.e, the mouse moves), then:

query the _input(ev) function
query the Control nodes
query _unhandled_input(ev)
finally, query the CollisionObject nodes (3D/2D camera casting)

This happens even with no code. Did you notice?

Especially in Godot, but also as standard practice, it’s generally the content of your code that is responsible for the major (lack of) performance, not so much where you are putting it.

From experience, I’m pretty confident you’ll find that your jittering issue lies elsewhere.

Yuminous | 2021-08-13 08:57

I experimented with different displays and it seems like the jitter is on higher hz monitors (above 60hz). I tried it on 60hz monitor and no jitter at all. I tried putting physics fps to 75 (my display hz) and the there was no jitter at all. Seems like putting higher physics fps solves the problem. That could be a solution?

20xxdd20 | 2021-08-13 09:14

This will have no effect whatsoever on the performance (or jitter), however, I am just noting that, in your code, you are multiplying by -1, when you can append a - before the deg2rad(), however, this is just my opinion, not sure what the preferred style is in GDScript, though). Anyway, hope you find the solution to your problem.

Snail0259 | 2021-08-13 10:37

Okay, so it sounds like you are having the physics interpolation jitter issue. This is something that is getting worked on engine-level and isn’t the fault of your project.

See this YouTube video for a demonstration of what it looks like:

And this is the current solution (from the video), which is an addon that was designed specifically to solve it:
GitHub - lawnjelly/smoothing-addon: Fixed timestep interpolation gdscript addon for Godot Engine (version 3.2 or later)

I believe you need to include the files in every project you have the issue in, but the installation process is relatively straightforward, quoting:

  1. Create a new project in Godot or use an existing project.
  2. Copy the ‘addons’ folder from this repository to your Godot project folder.
  3. Go to ‘Project Settings’ plugins tab.
  4. Find the smoothing plugin and set status to ‘Active’.

Hope it helps!

Yuminous | 2021-08-13 10:40