Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Spatial
- export var ZoomSpeed = 0.25
- export var RotateSpeed = 0.15
- export var MaxZoomDistance = 15.0
- export var MedZoomDistance = 6.0
- export var MinZoomDistance = 3.0
- export var MinClampXAngle = 70
- export var MaxClampXAngle = -35
- export var CameraYAdjust = 1.75
- var distance
- var target
- var player
- var zoom_level = 1
- # probe variables
- var left_probe
- var right_probe
- var back_probe
- var head_probe
- var butt_probe # I am SOOO mature...
- var probes = [] # this array holds the probes, so we can detect collision in a for loop
- var cam
- var occluded
- # Called when the node enters the scene tree for the first time.
- func _ready():
- # set as the top level
- set_as_toplevel(true)
- # store the player node (ALWAYS make sure the player is the DIRECT parent of YROTATE!!!)
- player = get_parent()
- # store the target node
- target = get_node("XRotate/Target")
- # hide the helper mesh
- $XRotate/HelperMesh.hide()
- # set the camera to the min distance at start
- target.transform.origin.z = MinZoomDistance
- # store the camera
- cam = $XRotate/InterpolatedCamera
- # store the probe nodes
- left_probe = $XRotate/InterpolatedCamera/Probes/Left
- right_probe = $XRotate/InterpolatedCamera/Probes/Right
- back_probe = $XRotate/InterpolatedCamera/Probes/Back
- head_probe = $XRotate/InterpolatedCamera/Probes/Head
- butt_probe = $XRotate/InterpolatedCamera/Probes/Butt
- # store the probes in an array
- probes = [left_probe, right_probe, back_probe, head_probe, butt_probe]
- # orient target to player
- target.look_at(player.transform.origin, Vector3(0, 1, 0))
- func _physics_process(delta):
- # set the current distance
- distance = target.transform.origin.z
- # get rotation input from controller
- var xRotateAmount = Input.get_action_strength("cam_down") - Input.get_action_strength("cam_up")
- var yRotateAmount = Input.get_action_strength("cam_right") - Input.get_action_strength("cam_left")
- # rotate xRotate node
- var cam_rot = $XRotate.rotation_degrees
- cam_rot.x = clamp(cam_rot.x, MaxClampXAngle, MinClampXAngle)
- $XRotate.rotation_degrees = cam_rot
- $XRotate.rotate_x(-xRotateAmount * RotateSpeed)
- # rotate y
- rotate_y(-yRotateAmount * RotateSpeed)
- # set the distance of the camera
- _update_camera()
- func _update_camera():
- var toggle_zoom = Input.is_action_just_pressed("toggle_distance")
- # if we toggle the zoom, increase the zoom level
- if toggle_zoom:
- zoom_level += 1
- if zoom_level > 3:
- zoom_level = 1
- # set the distance, depending on the zoom level
- if zoom_level == 1:
- target.transform.origin.z = MinZoomDistance
- elif zoom_level == 2:
- target.transform.origin.z = MedZoomDistance
- elif zoom_level == 3:
- target.transform.origin.z = MaxZoomDistance
- # check if the player is being occluded or not
- _detect_occlusion()
- # check for camera collisions
- if !occluded:
- _camera_collisions()
- func _detect_occlusion():
- var look_for_player = get_world().get_direct_space_state()
- var obstacle = look_for_player.intersect_ray(target.global_transform.origin, player.transform.origin)
- if !obstacle.empty():
- # store the previous position of the target node, so that we can return to it
- var prev_position = target.global_transform.origin
- # if the obstacle is not the player, move the target such that it isn't inside of the obstacle
- if !obstacle.collider.is_in_group("Player"):
- occluded = true
- print("hit object " + str(obstacle.collider) + " at " + str(obstacle.position))
- # move the target to the player
- target.global_transform.origin = player.transform.origin
- else:
- # restore the target's previous position
- target.global_transform.origin = prev_position
- occluded = false
- func _camera_collisions():
- var prev_global_position = target.global_transform.origin
- var prev_local_position = target.transform.origin
- # loop through the probes array and check if any of them collider with the environment
- for probe in probes:
- if probe.is_colliding():
- print(str(probe.get_collider()))
- # zoom in on the player
- target.transform.origin.z = MinZoomDistance-1
- # look at the player
- target.look_at(player.transform.origin, Vector3(0, 1, 0))
- else:
- target.transform.origin = prev_local_position
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement