From 7bfa7fcf02124a4508dffa9d78e8ef921b86a315 Mon Sep 17 00:00:00 2001 From: Lucas Fryzek Date: Sun, 17 Sep 2023 22:00:55 -0400 Subject: Setup basic player controller --- addons/third-person-camera/plugin.cfg | 7 + .../third_person_camera/ThirdPersonCamera.gd | 176 +++++++++++++++++++++ .../third_person_camera/ThirdPersonCamera.tscn | 49 ++++++ .../third_person_camera/ThirdPersonCameraIcon.svg | 6 + .../ThirdPersonCameraIcon.svg.import | 37 +++++ addons/third-person-camera/tpc.gd | 12 ++ 6 files changed, 287 insertions(+) create mode 100644 addons/third-person-camera/plugin.cfg create mode 100644 addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd create mode 100644 addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn create mode 100644 addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg create mode 100644 addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg.import create mode 100644 addons/third-person-camera/tpc.gd (limited to 'addons') diff --git a/addons/third-person-camera/plugin.cfg b/addons/third-person-camera/plugin.cfg new file mode 100644 index 0000000..60cb408 --- /dev/null +++ b/addons/third-person-camera/plugin.cfg @@ -0,0 +1,7 @@ +[plugin] + +name="Third Person Camera" +description="A Third Person Camera node for Godot4." +author="Jean KOUSSAWO" +version="1.1.0" +script="tpc.gd" diff --git a/addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd b/addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd new file mode 100644 index 0000000..a460835 --- /dev/null +++ b/addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd @@ -0,0 +1,176 @@ +@icon("./ThirdPersonCameraIcon.svg") +@tool +class_name ThirdPersonCamera extends Node3D + + +@onready var _camera := $Camera +@onready var _camera_rotation_pivot = $RotationPivot +@onready var _camera_offset_pivot = $RotationPivot/OffsetPivot +@onready var _camera_spring_arm := $RotationPivot/OffsetPivot/CameraSpringArm +@onready var _camera_marker := $RotationPivot/OffsetPivot/CameraSpringArm/CameraMarker + + + +## +@export var distance_from_pivot := 10.0 : + set(value) : + distance_from_pivot = value + $RotationPivot/OffsetPivot/CameraSpringArm.spring_length = distance_from_pivot + +## +@export var pivot_offset := Vector2.ZERO + +## +@export_range(-90.0, 90.0) var initial_dive_angle_deg := -20.0 : + set(value) : + initial_dive_angle_deg = clampf(value, tilt_lower_limit_deg, tilt_upper_limit_deg) + +## +@export_range(-90.0, 90.0) var tilt_upper_limit_deg := 60.0 + +## +@export_range(-90.0, 90.0) var tilt_lower_limit_deg := -60.0 + +## +@export_range(1.0, 1000.0) var tilt_sensitiveness := 10.0 + +## +@export_range(1.0, 1000.0) var horizontal_rotation_sensitiveness := 10.0 + +## +@export var current : bool = false : + set(value) : + $Camera.current = value + current = value + + +## +@export_group("mouse") +## +@export var mouse_follow : bool = false + +## +@export_range(0., 100.) var mouse_x_sensitiveness : float = 1 + +## +@export_range(0., 100.) var mouse_y_sensitiveness : float = 1 + + +# Camera3D properies replication +@export_category("Camera3D") +@export var keep_aspect : Camera3D.KeepAspect = Camera3D.KEEP_HEIGHT +@export_flags_3d_render var cull_mask : int = 1048575 +@export var environment : Environment +@export var attributes : CameraAttributes +@export var doppler_tracking : Camera3D.DopplerTracking = Camera3D.DOPPLER_TRACKING_DISABLED +@export var projection : Camera3D.ProjectionType = Camera3D.PROJECTION_PERSPECTIVE +@export_range(1.0, 179.0, 0.1, "suffix:°") var FOV = 75.0 +@export var near := 0.05 +@export var far := 4000.0 + + + +var camera_tilt_deg := 0. +var camera_horizontal_rotation_deg := 0. + + +func _ready(): + _camera.top_level = true + + +func _physics_process(_delta): + _update_camera_properties() + if Engine.is_editor_hint() : + _camera_marker.global_position = Vector3(0., 0., 1.).rotated(Vector3(1., 0., 0.), deg_to_rad(initial_dive_angle_deg)).rotated(Vector3(0., 1., 0.), deg_to_rad(-camera_horizontal_rotation_deg)) * _camera_spring_arm.spring_length + _camera_spring_arm.global_position + pass + #_camera.global_position = _camera_marker.global_position + tweenCameraToMarker() + _camera_offset_pivot.global_position = _camera_offset_pivot.get_parent().to_global(Vector3(pivot_offset.x, pivot_offset.y, 0.0)) + _camera_rotation_pivot.global_rotation_degrees.x = initial_dive_angle_deg + _camera_rotation_pivot.global_position = global_position + _process_tilt_input() + _process_horizontal_rotation_input() + _update_camera_tilt() + _update_camera_horizontal_rotation() + +func tweenCameraToMarker() : + var tween = create_tween() + tween.tween_property(_camera, "global_position", _camera_marker.global_position, 0.1) + +func _process_horizontal_rotation_input() : + if InputMap.has_action("tp_camera_right") and InputMap.has_action("tp_camera_left") : + var camera_horizontal_rotation_variation = Input.get_action_strength("tp_camera_right") - Input.get_action_strength("tp_camera_left") + camera_horizontal_rotation_variation = camera_horizontal_rotation_variation * get_process_delta_time() * 30 * horizontal_rotation_sensitiveness + camera_horizontal_rotation_deg += camera_horizontal_rotation_variation + + +func _process_tilt_input() : + if InputMap.has_action("tp_camera_up") and InputMap.has_action("tp_camera_down") : + var tilt_variation = Input.get_action_strength("tp_camera_up") - Input.get_action_strength("tp_camera_down") + tilt_variation = tilt_variation * get_process_delta_time() * 5 * tilt_sensitiveness + camera_tilt_deg = clamp(camera_tilt_deg + tilt_variation, tilt_lower_limit_deg - initial_dive_angle_deg, tilt_upper_limit_deg - initial_dive_angle_deg) + + + +func _update_camera_tilt() : + var tilt_final_val = clampf(initial_dive_angle_deg + camera_tilt_deg, tilt_lower_limit_deg, tilt_upper_limit_deg) + var tween = create_tween() + tween.tween_property(_camera, "global_rotation_degrees:x", tilt_final_val, 0.1) + + +func _update_camera_horizontal_rotation() : + # TODO : inverse + var tween = create_tween() + tween.tween_property(_camera_rotation_pivot, "global_rotation_degrees:y", camera_horizontal_rotation_deg * -1, 0.1).as_relative() + camera_horizontal_rotation_deg = 0.0 # reset the value + var vect_to_offset_pivot : Vector2 = ( + Vector2(_camera_offset_pivot.global_position.x, _camera_offset_pivot.global_position.z) + - + Vector2(_camera.global_position.x, _camera.global_position.z) + ).normalized() + _camera.global_rotation.y = -Vector2(0., -1.).angle_to(vect_to_offset_pivot.normalized()) + + + + +func _unhandled_input(event): + if mouse_follow and event is InputEventMouseMotion: + camera_horizontal_rotation_deg += event.relative.x * 0.1 * mouse_x_sensitiveness + camera_tilt_deg -= event.relative.y * 0.07 * mouse_y_sensitiveness + return + + pass + + +func _update_camera_properties() : + _camera.keep_aspect = keep_aspect + _camera.cull_mask = cull_mask + _camera.doppler_tracking = doppler_tracking + _camera.projection = projection + _camera.fov = FOV + _camera.near = near + _camera.far = far + if _camera.environment != environment : + _camera.environment = environment + if _camera.attributes != attributes : + _camera.attributes = attributes + + +func get_camera() : + return $Camera + + +func get_front_direction() : + var dir : Vector3 = _camera_offset_pivot.global_position - _camera.global_position + dir.y = 0. + dir = dir.normalized() + return dir + +func get_back_direction() : + return -get_front_direction() + +func get_left_direction() : + return get_front_direction().rotated(Vector3.UP, PI/2) + +func get_right_direction() : + return get_front_direction().rotated(Vector3.UP, -PI/2) diff --git a/addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn b/addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn new file mode 100644 index 0000000..f1faf2e --- /dev/null +++ b/addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn @@ -0,0 +1,49 @@ +[gd_scene load_steps=6 format=3 uid="uid://wmf2eu0uuhrg"] + +[ext_resource type="Script" path="res://addons/third-person-camera/third_person_camera/ThirdPersonCamera.gd" id="1_telmq"] + +[sub_resource type="SeparationRayShape3D" id="SeparationRayShape3D_84uqy"] +margin = 1.135 + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2bjii"] +albedo_color = Color(0.8, 0.686275, 0.215686, 1) + +[sub_resource type="SphereMesh" id="SphereMesh_ag7lb"] +material = SubResource("StandardMaterial3D_2bjii") +radius = 0.05 +height = 0.1 + +[sub_resource type="CylinderMesh" id="CylinderMesh_ybkhn"] +top_radius = 0.0 +height = 0.938 + +[node name="ThirdPersonCamera" type="Node3D"] +script = ExtResource("1_telmq") + +[node name="RotationPivot" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 0.939693, 0.34202, 0, -0.34202, 0.939693, 0, 0, 0) +top_level = true + +[node name="OffsetPivot" type="Node3D" parent="RotationPivot"] +transform = Transform3D(1, -3.9187e-07, 6.47546e-10, 3.94476e-07, 1, 5.65946e-05, -2.27374e-11, -5.65946e-05, 1, 0, 0, 0) + +[node name="CameraSpringArm" type="SpringArm3D" parent="RotationPivot/OffsetPivot"] +process_priority = 11 +shape = SubResource("SeparationRayShape3D_84uqy") +spring_length = 10.0 + +[node name="CameraMarker" type="Marker3D" parent="RotationPivot/OffsetPivot/CameraSpringArm"] +transform = Transform3D(1, 7.93407e-08, 3.5101e-07, 1.48521e-08, 1, -7.89762e-06, 2.08538e-07, 1.2219e-06, 1, -6.69741e-09, -0.000566006, 9.99999) + +[node name="PivotDebug" type="MeshInstance3D" parent="RotationPivot/OffsetPivot"] +visible = false +mesh = SubResource("SphereMesh_ag7lb") + +[node name="Camera" type="Camera3D" parent="."] +transform = Transform3D(1, 5.38245e-15, -1.47882e-14, 0, 0.939693, 0.34202, 1.57372e-14, -0.34202, 0.939693, -1.47882e-13, 3.4202, 9.39693) +top_level = true + +[node name="CameraDebug" type="MeshInstance3D" parent="Camera"] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0, 0.570638) +visible = false +mesh = SubResource("CylinderMesh_ybkhn") diff --git a/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg b/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg new file mode 100644 index 0000000..b5944dc --- /dev/null +++ b/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg @@ -0,0 +1,6 @@ + + + + + + diff --git a/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg.import b/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg.import new file mode 100644 index 0000000..fa3b162 --- /dev/null +++ b/addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg.import @@ -0,0 +1,37 @@ +[remap] + +importer="texture" +type="CompressedTexture2D" +uid="uid://dgbqe4e8m5dlo" +path="res://.godot/imported/ThirdPersonCameraIcon.svg-895880fcccc5c0191c5c521669ff32ad.ctex" +metadata={ +"vram_texture": false +} + +[deps] + +source_file="res://addons/third-person-camera/third_person_camera/ThirdPersonCameraIcon.svg" +dest_files=["res://.godot/imported/ThirdPersonCameraIcon.svg-895880fcccc5c0191c5c521669ff32ad.ctex"] + +[params] + +compress/mode=0 +compress/high_quality=false +compress/lossy_quality=0.7 +compress/hdr_compression=1 +compress/normal_map=0 +compress/channel_pack=0 +mipmaps/generate=false +mipmaps/limit=-1 +roughness/mode=0 +roughness/src_normal="" +process/fix_alpha_border=true +process/premult_alpha=false +process/normal_map_invert_y=false +process/hdr_as_srgb=false +process/hdr_clamp_exposure=false +process/size_limit=0 +detect_3d/compress_to=1 +svg/scale=1.0 +editor/scale_with_editor_scale=false +editor/convert_colors_with_editor_theme=false diff --git a/addons/third-person-camera/tpc.gd b/addons/third-person-camera/tpc.gd new file mode 100644 index 0000000..45a417e --- /dev/null +++ b/addons/third-person-camera/tpc.gd @@ -0,0 +1,12 @@ +@tool +extends EditorPlugin + + +func _enter_tree(): + # Initialization of the plugin goes here. + pass + + +func _exit_tree(): + # Clean-up of the plugin goes here. + pass -- cgit