Godot Version
4.3
Question
I want the camera to rotate with the player but the camera cant identify the player node, can anyone help me on this?
extends Camera2D
# Reference to the player
@onready var Player2D: CharacterBody2D = $CharacterBody2D
# Offset to maintain a distance from the player
# Update function
func _process(_delta: float) -> void:
if $CharacterBody2D:
# Camera Postion (based on the player's postion) + an offest
position = $CharacterBody2D.position + offset.rotated($CharacterBody2D.rotation)
# Camera Rotation that matches Player Rotation
rotation = $CharacterBody2D.rotation
How does your scenetree look like?
okay instead of onready use export:
@export var Player2D: CharacterBody2D
Then go to the inspector on the camera and select the player as Player2D
EDIT:
General rule of thumb is to use onready on children and export on siblings and parents
1 Like
first, your camera is trying to reference player, but is child of player. that should throw an error.
second, you are trying to change position
. position
is relative to the parent node. a child node is moved by the parent and position shifts/adds its position to it. so this code will never work.
either put your camera and player parallel to each other (as siblings), and get the player with an export
, or use global_position
, although in this particular case it would be best if the camera where to be controlled from the player.
player.gd
$Camera2D.rotation = rotation
also, this code is wrong:
you are using $CharacterBody2D
, which is a method for obtaining a node. you already do this here:
this is a reference and the name of the reference is Player2D
, so it should be:
rotation = Player2D.rotation
also, use snake_case
for variables and functions, as per the gdscript style guide. don’t capitalize variable names, capitalization (PascalCase
) is reserved for class names. this will save you a lot of headaches in the future.
player_2d
instead of Player2D
1 Like
Im not very experienced with Godot that much, could you help me figure how to structure this whole thing correctly? Been struggling for 4 hours.
To fix your code it would have to look like this:
extends Camera2D
# Reference to the player
@onready var player2D: CharacterBody2D = $"../CharacterBody2D"
# Offset to maintain a distance from the player
# Update function
func _process(_delta: float) -> void:
if player2d:
# Camera Postion (based on the player's postion) + an offest
position = player2D.position + offset.rotated(player2D.rotation)
# Camera Rotation that matches Player Rotation
rotation = player2D.rotation
First of all the camera automatically follows your player if its a child of it, second of all as said above i recommed to use export on parents and siblings
1 Like
where do I put
$Camera2D.rotation = rotation
?
you don’t, it was an example.
I’m not giving you complete code to copy-paste in your project, what you want to do and how you do it are different things.
my first question would be, where did you get this code from? did you write it yourself? I see some comments, but it’s not valid code, or at least there’s no reference to offset.
it seems you are very new not just to godot but to coding.
I recommend you go slowly, this is already very basic, but you are using references. try changing position, rotation, etc of a single node first. maybe follow a tutorial?
learn what each of the things do before jumping to something more complex.
and pay attention to the errors and warnings in the console, they will tell you what is wrong with your code.