Prevent gameplay camera from getting stuck on walls.
This commit is contained in:
		
							parent
							
								
									8fc34c825b
								
							
						
					
					
						commit
						b5a2309e6b
					
				| 
						 | 
				
			
			@ -6,6 +6,7 @@ class_name GameplayCamera extends Camera3D
 | 
			
		|||
@export var target_max_distance: float = 10.0
 | 
			
		||||
@export var target_decentering_factor: float = 0.4
 | 
			
		||||
@export var target_leveling_bias: float = 20.0
 | 
			
		||||
@export var allow_losing_target := false
 | 
			
		||||
@export var follow_lerp_speed: float = 0.999
 | 
			
		||||
@export var turn_lerp_speed: float = 0.999
 | 
			
		||||
@export var fov_lerp_speed: float = 0.999
 | 
			
		||||
| 
						 | 
				
			
			@ -81,20 +82,31 @@ func _respond_to_impetus(delta: float) -> void:
 | 
			
		|||
	)
 | 
			
		||||
	look_impetus = Vector3.ZERO
 | 
			
		||||
 | 
			
		||||
func _try_go(where: Vector3) -> void:
 | 
			
		||||
	_raycast_object.from = global_position
 | 
			
		||||
func _try_go(where: Vector3, from: Vector3 = global_position) -> void:
 | 
			
		||||
	_raycast_object.from = from
 | 
			
		||||
	_raycast_object.to = where + (
 | 
			
		||||
		(where - global_position).normalized()*clip_margin
 | 
			
		||||
	)
 | 
			
		||||
	if target:
 | 
			
		||||
		_raycast_object.exclude = [target]
 | 
			
		||||
	else:
 | 
			
		||||
		_raycast_object.exclude = []
 | 
			
		||||
	var raycast_results := (
 | 
			
		||||
		get_world_3d().direct_space_state.intersect_ray(_raycast_object)
 | 
			
		||||
	)
 | 
			
		||||
	var desired_position: Vector3
 | 
			
		||||
	if raycast_results.is_empty():
 | 
			
		||||
		global_position = where
 | 
			
		||||
		desired_position = where
 | 
			
		||||
	else:
 | 
			
		||||
		global_position = raycast_results.position + (
 | 
			
		||||
		desired_position = raycast_results.position + (
 | 
			
		||||
			raycast_results.normal*clip_margin
 | 
			
		||||
		)
 | 
			
		||||
	if target && !allow_losing_target:
 | 
			
		||||
		allow_losing_target = true
 | 
			
		||||
		_try_go(desired_position, target.global_position)
 | 
			
		||||
		allow_losing_target = false
 | 
			
		||||
	else:
 | 
			
		||||
		global_position = desired_position
 | 
			
		||||
 | 
			
		||||
func snap_to_target() -> void:
 | 
			
		||||
	if !target: return
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue