dude_in_a_basement
dude_in_a_basement

Reputation: 27

Why is the control node affecting my camera3d movement in Godot 4.2

Here is the camera movement code, right next to the control node The code for the controll node doesn affect the camera movement, just the control node itself

Upvotes: 1

Views: 263

Answers (1)

Theraot
Theraot

Reputation: 40220

Let us engage in the ancient art of reading the code....


Line 1:

extends Node3D

Tells us that this script will be attached to a Node3D. It also tell us that there will be Node3D nodes that extended by means of having this script.


Line 3:

@onready var player = $".."

Tell us that when the Node3D that has this ndoe attached becomes ready, we will initialize a variable player with a reference to the parent node.

This implies that the parent node is representing the player. And thus, we know that the Node3D that has this script attached is intended to be placed as a child of the player.


Lines 5 to 7 (ignoring comments):

func _ready():

    Input.mouse_mode = Input.MOUSE_MODE_CAPTURE

This tells us that when the Node3D that has this script attached becomes ready, it will tell Godot to capture the mouse pointer in the game window.

Which is not what the comment says.


Lines 9 to 14 (ignoring comments):

func _unhandled_input(event):

    if event is InputEventMouseMotion:
        player.rotate_y(-event.relative.x * 0.01)
        rotate_x(-event.relative.y * 0.01)
        rotation.x = clamp(rotation.x, deg_to_rad(-90), deg_to_rad(90))

This tells us that when there is an input event that was not marked handled somewhere else, and that input event is the motion of the mouse, the script will tell Godot to:

  • Rotate the player around the y axis (which is the vertical axis) proportionally to the horizontal motion of the mouse.
  • Rotate the Node3D that has this script attached around the x axis... And also clamp that rotation to the range between -90 degrees to 90 degrees.

Despite what the comment says, no camera is involved.


Thus, the code does not handle a camera.

Also, it appears that no camera is a child of the Node3D that has this code attached.

And that is why said Node3D does not affect the movement of your Camera3D.


I presume you would also be interested in how can you make it affect the movement of your Camera3D.

There are two simple ways:

  • You can make your Camera3D a child of the Node3D that has this script attached, so that when it rotates, the Camera3D also rotates by virtue of being its child.

  • Get a reference your Camera3D (which you might be able to do with the $ syntax, or by getting it from get_viewport().get_camera_3d()) and move it similar fashion as the way the script manipulates player. This will give you more control over the motion of the Camera3D, but also means more ways you can get it wrong... And you need to write the code for what you want exactly.

There are also two less simple approaches:

  • You can give the Camera3D another script, which will control its motion. Which might either use _unhandled_input itself... Or might get a reference to the Node3D that has this script attached (for example, using the $ syntax) and copy the motion from it.

  • And while we are talking about copying the motion of another Node3D... You might use a RemoteTransform3D to so for you. So you can set the RemoteTransform3D to move the Camera3D, and thus the Camera3D will move with the Node3D that has this script attached, without being a child.

Upvotes: 1

Related Questions