Hello, how do you move a camara 3d with scrpit when a player enters an area 3d?

Godot Version

4.4

Question

well that im working on something and testing some stuff but im not sure if im doing in correctly, do you know how to move a camara that is the child of a player scene when the player enters an area 3d

If the player has a reference to the camera such as a @onready var then you can access that from the Area3D

# player script
@onready var camera: Camera3D = $Camera3D

# area script
func _on_body_entered(body: Node3D) -> void:
    if body is Player:
        body.camera.position = new_position
1 Like

how do i make the scrpit understand he is the player

You can attach a script to the player, assuming it is a physics body like CharacterBody3D then the Area3D can detect it through it’s body_entered signal.

1 Like

yes the player has script and is a character body 3d, but the code you told me should be put on the area 3d or the player?

the player already has a @onready var camera: Camera3D = $Camera3D

# player script
@onready var camera: Camera3D = $Camera3D

# area script
func _on_body_entered(body: Node3D) -> void:
    if body is Player:
        body.camera.position = new_position

If the player already has a camera variable then you do not need to add that line under “player script”

The “Area script” should be attached to your Area3D

extends Area3D

@onready var player: CharacterBody3D = $"../Player" = "player"


# area script
func _on_body_entered(body: Node3D) -> void:
    if body is player:
        body.camera.position = new_position
![Captura de pantalla 2025-05-13 183350|690x102](upload://9o7OEC8qNvaoiNsnt1l2jM3T7DU.png)

Ah I see, there are many ways to filter collisions, I like to filter by type/class_name using the is keyword to detect types. You could use the node’s name if that makes more sense for you.

func _on_body_entered(body: Node3D) -> void:
    if body.name == "player": # check by node name

You also must assign a new_position, I made that sample without possibly knowing where you want the camera to go so you must fill in that value yourself.

1 Like

Godot does like mixing tabs and spaces, I wouldn’t recommend copying script from the forum as they may use different indentation than you, notice line 7 has two grey arrows → representing tabs, where line 6 does not.

You cannot assign variables within a function, and new_position is not a defined function, if you want to hard-code a number that way you can create a new vector 3

body.camera.position = Vector3(0, 5.162, 22)

Make sure you actually connect the signal too, copying code does not automatically connect to the body_enetered signal. You will see a green icon to the left of the function definition if you did connect it.

i tried re connecting a signal to the body but it sends the code to the player script

I’m not sure how your scene is set up but you can select which node to attach the signal to, assuming the node has a script.



i have it like this

Click “room4 (Connecting From)”

i did its the same like the code is now on the area 3d but its still red

That’s good if the code is the same, but what errors do you recieve?

mixed use of tabs and spaces for indantantion

You need to fix the indentation, either use all spaces or all tabs at the start of a line.

extends Area3D


func _on_body_entered(body: Node3D) -> void:
 if body.name == "player":body.camera.position = Vector3(0, 5.162, 22)

now i just need to give it a proper acces to the camera

This tells you that your player script, specifically playertype2.gd does not have a camera variable as discussed. Can you paste the pointed out player script?