From dad6e2f73eaee2ba01d8ee5504f8b94954bc1d79 Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Tue, 15 Apr 2025 15:26:38 +0200 Subject: [PATCH] fps sample setup --- project.godot | 36 +++++++++++++++++++ scenes/game.tscn | 20 +++++++++++ scenes/ground.tscn | 18 ++++++++++ scenes/player.tscn | 21 +++++++++++ scripts/player.gd | 26 ++++++++++++++ scripts/player.gd.uid | 1 + scripts/player_camera.gd | 68 ++++++++++++++++++++++++++++++++++++ scripts/player_camera.gd.uid | 1 + 8 files changed, 191 insertions(+) create mode 100644 scenes/game.tscn create mode 100644 scenes/ground.tscn create mode 100644 scenes/player.tscn create mode 100644 scripts/player.gd create mode 100644 scripts/player.gd.uid create mode 100644 scripts/player_camera.gd create mode 100644 scripts/player_camera.gd.uid diff --git a/project.godot b/project.godot index 5cc698b..6b431ee 100644 --- a/project.godot +++ b/project.godot @@ -11,5 +11,41 @@ config_version=5 [application] config/name="parable" +run/main_scene="uid://c2kjfysadph28" config/features=PackedStringArray("4.4", "Forward Plus") config/icon="res://icon.svg" + +[input] + +ui_left={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194319,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":-1.0,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":65,"key_label":0,"unicode":97,"location":0,"echo":false,"script":null) +] +} +ui_right={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194321,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":14,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":0,"axis_value":1.0,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":68,"key_label":0,"unicode":100,"location":0,"echo":false,"script":null) +] +} +ui_up={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194320,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":11,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":-1.0,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":87,"key_label":0,"unicode":119,"location":0,"echo":false,"script":null) +] +} +ui_down={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":4194322,"physical_keycode":0,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) +, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":12,"pressure":0.0,"pressed":false,"script":null) +, Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":0,"axis":1,"axis_value":1.0,"script":null) +, Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":83,"key_label":0,"unicode":115,"location":0,"echo":false,"script":null) +] +} diff --git a/scenes/game.tscn b/scenes/game.tscn new file mode 100644 index 0000000..ef4d5d6 --- /dev/null +++ b/scenes/game.tscn @@ -0,0 +1,20 @@ +[gd_scene load_steps=4 format=3 uid="uid://c2kjfysadph28"] + +[ext_resource type="Texture2D" uid="uid://bsd3o6sy0cdc5" path="res://icon.svg" id="1_uwrxv"] +[ext_resource type="PackedScene" uid="uid://dsgumv1053wb2" path="res://scenes/player.tscn" id="2_lnu2h"] +[ext_resource type="PackedScene" uid="uid://dfbqg6cxhbpng" path="res://scenes/ground.tscn" id="3_lbhrr"] + +[node name="Game" type="Node3D"] + +[node name="Godot" type="Sprite3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -2.16255) +texture = ExtResource("1_uwrxv") + +[node name="Player" parent="." instance=ExtResource("2_lnu2h")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.73814, 0) + +[node name="Ground" parent="." instance=ExtResource("3_lbhrr")] +transform = Transform3D(100, 0, 0, 0, 100, 0, 0, 0, 100, 0, 0, 0) + +[node name="Sun" type="DirectionalLight3D" parent="."] +transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 7.72306, 0) diff --git a/scenes/ground.tscn b/scenes/ground.tscn new file mode 100644 index 0000000..c307d9b --- /dev/null +++ b/scenes/ground.tscn @@ -0,0 +1,18 @@ +[gd_scene load_steps=4 format=3 uid="uid://dfbqg6cxhbpng"] + +[sub_resource type="PlaneMesh" id="PlaneMesh_7ylpw"] + +[sub_resource type="PhysicsMaterial" id="PhysicsMaterial_7ylpw"] + +[sub_resource type="BoxShape3D" id="BoxShape3D_pybca"] +size = Vector3(2, 0.2, 2) + +[node name="Ground" type="MeshInstance3D"] +mesh = SubResource("PlaneMesh_7ylpw") + +[node name="StaticBody3D" type="StaticBody3D" parent="."] +physics_material_override = SubResource("PhysicsMaterial_7ylpw") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.099587, 0) +shape = SubResource("BoxShape3D_pybca") diff --git a/scenes/player.tscn b/scenes/player.tscn new file mode 100644 index 0000000..6284f72 --- /dev/null +++ b/scenes/player.tscn @@ -0,0 +1,21 @@ +[gd_scene load_steps=5 format=3 uid="uid://dsgumv1053wb2"] + +[ext_resource type="Script" uid="uid://ccj8w85qobhpl" path="res://scripts/player.gd" id="1_3vyb7"] +[ext_resource type="Script" uid="uid://bs7pcycdeiwqx" path="res://scripts/player_camera.gd" id="2_g2els"] + +[sub_resource type="CapsuleMesh" id="CapsuleMesh_u8vuu"] + +[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_u8vuu"] + +[node name="Player" type="CharacterBody3D"] +script = ExtResource("1_3vyb7") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="."] +mesh = SubResource("CapsuleMesh_u8vuu") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] +shape = SubResource("CapsuleShape3D_u8vuu") + +[node name="Camera" type="Camera3D" parent="."] +current = true +script = ExtResource("2_g2els") diff --git a/scripts/player.gd b/scripts/player.gd new file mode 100644 index 0000000..81afafb --- /dev/null +++ b/scripts/player.gd @@ -0,0 +1,26 @@ +extends CharacterBody3D + +const SPEED = 5.0 +const JUMP_VELOCITY = 4.5 + +func _physics_process(delta): + # Add the gravity. + if not is_on_floor(): + velocity += get_gravity() * delta + + # Handle jump. + if Input.is_action_just_pressed("ui_accept") and is_on_floor(): + velocity.y = JUMP_VELOCITY + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down") + var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized() + if direction: + velocity.x = direction.x * SPEED + velocity.z = direction.z * SPEED + else: + velocity.x = move_toward(velocity.x, 0, SPEED) + velocity.z = move_toward(velocity.z, 0, SPEED) + + move_and_slide() diff --git a/scripts/player.gd.uid b/scripts/player.gd.uid new file mode 100644 index 0000000..2008213 --- /dev/null +++ b/scripts/player.gd.uid @@ -0,0 +1 @@ +uid://ccj8w85qobhpl diff --git a/scripts/player_camera.gd b/scripts/player_camera.gd new file mode 100644 index 0000000..32969a6 --- /dev/null +++ b/scripts/player_camera.gd @@ -0,0 +1,68 @@ +# taken from +# https://github.com/tavurth/godot-simple-fps-camera/blob/master/player/Camera.gd +# +extends Camera3D + +@onready var Player = get_parent() + +## Increase this value to give a slower turn speed +const CAMERA_TURN_SPEED = 400 + +func _ready(): + ## Tell Godot that we want to handle input + set_process_input(true) + +func look_updown_rotation(new_rotation = 0): + """ + Returns a new Vector3 which contains only the x direction + We'll use this vector to compute the final 3D rotation later + """ + var toReturn = self.get_rotation() + Vector3(new_rotation, 0, 0) + + ## + ## We don't want the player to be able to bend over backwards + ## neither to be able to look under their arse. + ## Here we'll clamp the vertical look to 90° up and down + toReturn.x = clamp(toReturn.x, PI / -2, PI / 2) + + return toReturn + +func look_leftright_rotation(new_rotation = 0): + """ + Returns a new Vector3 which contains only the y direction + We'll use this vector to compute the final 3D rotation later + """ + return Player.get_rotation() + Vector3(0, new_rotation, 0) + +func _input(event): + """ + First person camera controls + """ + ## + ## We'll only process mouse motion events + if not event is InputEventMouseMotion: + return + + ## + ## We'll use the parent node "Player" to set our left-right rotation + ## This prevents us from adding the x-rotation to the y-rotation + ## which would result in a kind of flight-simulator camera + Player.set_rotation(look_leftright_rotation(event.relative.x / -CAMERA_TURN_SPEED)) + + ## + ## Now we can simply set our y-rotation for the camera, and let godot + ## handle the transformation of both together + self.set_rotation(look_updown_rotation(event.relative.y / -CAMERA_TURN_SPEED)) + +func _enter_tree(): + """ + Hide the mouse when we start + """ + Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN) + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + +func _leave_tree(): + """ + Show the mouse when we leave + """ + Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) diff --git a/scripts/player_camera.gd.uid b/scripts/player_camera.gd.uid new file mode 100644 index 0000000..078685e --- /dev/null +++ b/scripts/player_camera.gd.uid @@ -0,0 +1 @@ +uid://bs7pcycdeiwqx