3. Camera Rotation & Control
Implementeer orbital camera met keyboard rotation en camera-relative movement
Overzicht
Camera rotation input
using Godot;
public partial class View : Node3D
{
[Export] private Node3D _target;
[Export] private Camera3D _camera;
[Export] private float _followSpeed = 5.0f;
[Export] private float _rotationSpeed = 2.0f;
// .. de rest van de code
}public override void _PhysicsProcess(double delta)
{
if (_target == null) return;
// Smooth follow
GlobalPosition = GlobalPosition.Lerp(_target.GlobalPosition, _followSpeed * (float)delta);
// Camera rotation input
float horizontalInput = Input.GetAxis("camera_left", "camera_right");
float verticalInput = Input.GetAxis("camera_up", "camera_down");
}public override void _PhysicsProcess(double delta)
{
if (_target == null) return;
// Smooth follow
GlobalPosition = GlobalPosition.Lerp(_target.GlobalPosition, _followSpeed * (float)delta);
// Camera rotation input
float horizontalInput = Input.GetAxis("camera_left", "camera_right");
float verticalInput = Input.GetAxis("camera_up", "camera_down");
// Horizontal rotation (Y-axis)
RotateY(horizontalInput * _rotationSpeed * (float)delta);
// Vertical rotation (X-axis)
Rotate(Transform.Basis.X, verticalInput * _rotationSpeed * (float)delta);
}public override void _PhysicsProcess(double delta)
{
if (_target == null) return;
// Smooth follow
GlobalPosition = GlobalPosition.Lerp(_target.GlobalPosition, _followSpeed * (float)delta);
// Camera rotation input
float horizontalInput = Input.GetAxis("camera_left", "camera_right");
float verticalInput = Input.GetAxis("camera_up", "camera_down");
// Horizontal rotation (Y-axis)
RotateY(horizontalInput * _rotationSpeed * (float)delta);
// Vertical rotation (X-axis)
Rotate(Transform.Basis.X, verticalInput * _rotationSpeed * (float)delta);
// Clamp vertical rotation
Vector3 rotation = Rotation;
rotation.X = Mathf.Clamp(rotation.X, Mathf.DegToRad(-80), Mathf.DegToRad(-10));
Rotation = rotation;
}Test camera rotation
Camera-Relative Movement
Player camera target instellen
Test camera-relative movement
Complete scripts
Laatst bijgewerkt