generated from krampus/template-godot4
	
		
			
	
	
		
			22 lines
		
	
	
		
			737 B
		
	
	
	
		
			GDScript3
		
	
	
	
	
	
		
		
			
		
	
	
			22 lines
		
	
	
		
			737 B
		
	
	
	
		
			GDScript3
		
	
	
	
	
	
|  | class_name CameraController extends Node3D | ||
|  | ## Simple first-person camera motion controller | ||
|  | 
 | ||
|  | const PITCH_LIMIT := deg_to_rad(85.0) | ||
|  | 
 | ||
|  | @onready var _target := Vector2(rotation.x, rotation.y) | ||
|  | 
 | ||
|  | 
 | ||
|  | ## Rotate the camera by the given motion vector. | ||
|  | ## | ||
|  | ## The input motion vector is the change in pitch and yaw in radians. | ||
|  | func camera_motion(motion: Vector2) -> void: | ||
|  | 	_target.y -= motion.x | ||
|  | 	_target.x = clampf(_target.x - motion.y, -PITCH_LIMIT, PITCH_LIMIT) | ||
|  | 
 | ||
|  | 
 | ||
|  | func _physics_process(delta: float) -> void: | ||
|  | 	var accel: float = ProjectSettings.get_setting("game/config/input/camera_acceleration") | ||
|  | 	var weight := 1.0 - exp(-accel * delta) | ||
|  | 	rotation.y = lerp_angle(rotation.y, _target.y, weight) | ||
|  | 	rotation.x = lerp_angle(rotation.x, _target.x, weight) |