Cammera movement issues

Godot Version

4.4

Question

I´m very new to 3d game making, and i´m trying to make my camera move with Area3d´s that are children of the camera. imagine a binding of issac camera, but 3d, and its streets in a city, and in a single camera. so far i have managed to make it move left and right, but, i am having issue with the rotation, and wanted to make the movement smooth instead of just straight teleporting. Code bellow belongs to the Area3d, with the camera as a parent

extends Area3D

@export var X : float = 9.0
@export var Z : float = 9.0
@export var Angle : float = 0.0
var CurrentAngle : float = 0.0

func _on_body_entered(body):
	get_parent().position.x += (X)
	get_parent().position.z += (Z)
	
	CurrentAngle += Angle
	if CurrentAngle == 360 or -360:
		CurrentAngle = 0
	get_parent().rotation_degrees.y += (Angle)
		
	

What you’re describing I believe is a PhantomCamera (or a Cinemachine in Unity). Check out this plugin and see if it suits your needs. No need to reinvent the wheel for a solved problem :slight_smile:

1 Like

I also think a phantom camera plugin should be ok in your case, although I neve used that plugin, but I want to make up clear something really strange in your code and structure, so that you could remember for future code:

  1. if you move the camera, you also move all of its children, like the are3D, I hope this is intentional
  2. avoid direct changes on parent node: you could add a script to your camera where you connect area3D’s body_entered signal to a function that changes the position. This way you always know where the changes are from when you meet some stange behaviour. Also, the parent nodes are created after children, this could cause crashes on start/ object creation
  3. to loop angle values you can use the (built-in) function wrapf(). in your case:
    CurrentAngle = wrapf(CurrentAngle + Angle, -360, 360)
    but I think you could also use wrapf(CurrentAngle + Angle, 0, 360)
  4. about rotation, you need to know trigonometry and that’s math. There probably is some way to avoid using trigonometric functions directly by using local rotation but still, you would miss how it works and thus how to solve bugs. I permit myself to strongly suggest you to study trigonometry, at least which functions are the most common (sin, cos, tan) and their behaviour, first in 2D and then in 3D. Also, remember that Godot uses radians for angles.
    In your code it seems you only rotate around y-axis. in that case it should be sufficient to use (Xcos(get_parent().rotation.y) and (Zsin(get_parent().rotation.y) instead of (X) and (Z).
    This is probably to put after the angle change.
  5. the smooth movement is obtained by interpolation, that is, computing the “missing” points between the first one and the last one. The most common and easiest one is the linear interpolation, where the points are computed on the line that connects the first and last points.
    You could implement your own interpolation, and that is not difficult, but Godot also has its own function for that: lerp(). You use lerp() inside _physics_process(). Example:
    position = position.lerp(final_position, delta * camera_speed)