implemented basic camera for 3rd person controls

This commit is contained in:
Jonas Weinz 2022-08-13 19:35:08 +02:00
parent 1edbdc3dea
commit e8cfb39f6e
5 changed files with 116 additions and 27 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# --> vscode
.vscode/
# ---> Godot
# Godot-specific ignores
.import/

View File

@ -0,0 +1,44 @@
[remap]
importer="scene"
importer_version=1
type="PackedScene"
uid="uid://ds8m7wv4s25s4"
path="res://.godot/imported/robot.blend-000de9eae232532a839543b86cedcbd7.scn"
[deps]
source_file="res://blender/robot.blend"
dest_files=["res://.godot/imported/robot.blend-000de9eae232532a839543b86cedcbd7.scn"]
[params]
nodes/root_type="Node3D"
nodes/root_name="Scene Root"
nodes/root_scale=1.0
meshes/ensure_tangents=true
meshes/generate_lods=true
meshes/create_shadow_meshes=true
meshes/light_baking=1
meshes/lightmap_texel_size=0.2
skins/use_named_skins=true
animation/import=true
animation/fps=30
import_script/path=""
_subresources={}
blender/nodes/visible=2
blender/nodes/punctual_lights=true
blender/nodes/cameras=true
blender/nodes/custom_properties=true
blender/nodes/modifiers=1
blender/meshes/colors=false
blender/meshes/uvs=true
blender/meshes/normals=true
blender/meshes/tangents=true
blender/meshes/skins=2
blender/meshes/export_bones_deforming_mesh_only=false
blender/materials/unpack_enabled=true
blender/materials/export_materials=1
blender/animation/limit_playback=true
blender/animation/always_sample=true
blender/animation/group_tracks=true

View File

@ -1,41 +1,40 @@
[gd_scene load_steps=8 format=3 uid="uid://cuohiydexf5n"]
[gd_scene load_steps=9 format=3 uid="uid://cuohiydexf5n"]
[ext_resource type="PackedScene" uid="uid://c4ye4j37eiuhb" path="res://test_ground.tscn" id="1_p7ywp"]
[ext_resource type="Script" path="res://player.gd" id="2_f4o04"]
[sub_resource type="PhysicalSkyMaterial" id="PhysicalSkyMaterial_mh53i"]
[sub_resource type="Sky" id="Sky_fu4y1"]
sky_material = SubResource("PhysicalSkyMaterial_mh53i")
[sub_resource type="Environment" id="Environment_lamky"]
background_mode = 2
background_color = Color(0, 0.611765, 0.533333, 1)
sky = SubResource("Sky_fu4y1")
sky_custom_fov = 1.2
[ext_resource type="Script" path="res://player_camera.gd" id="3_as6nl"]
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_t1cxb"]
albedo_color = Color(0.0862745, 0.470588, 0.839216, 1)
[sub_resource type="SphereShape3D" id="SphereShape3D_sjmr4"]
[sub_resource type="PhysicalSkyMaterial" id="PhysicalSkyMaterial_ffy4a"]
[sub_resource type="Sky" id="Sky_8qk6q"]
sky_material = SubResource("PhysicalSkyMaterial_ffy4a")
[sub_resource type="Environment" id="Environment_srrj6"]
background_mode = 2
sky = SubResource("Sky_8qk6q")
ambient_light_source = 3
reflected_light_source = 2
[node name="Node3D" type="Node3D"]
[node name="test_ground" parent="." instance=ExtResource("1_p7ywp")]
transform = Transform3D(0.9994, -0.0346379, 0, 0.0346379, 0.9994, 0, 0, 0, 1, -1.52052, -9.32694, -0.309357)
[node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(0.00469292, -0.310288, 0.950631, -0.0542184, 0.949164, 0.310077, -0.998518, -0.0529968, -0.0123689, 80.7546, 36.3879, 1.88406)
environment = SubResource("Environment_lamky")
current = true
[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
transform = Transform3D(0.0657009, 0.989589, 0.12805, 0.676529, -0.138506, 0.723274, 0.73348, 0.0391095, -0.678585, 21.9756, 11.6848, 1.12691)
transform = Transform3D(-0.370338, 0.860141, -0.350723, 0.466547, 0.498728, 0.730482, 0.803233, 0.106897, -0.585995, 21.9756, 11.6848, 1.12691)
light_energy = 0.41
light_angular_distance = 8.26
directional_shadow_mode = 0
[node name="player" type="CharacterBody3D" parent="."]
[node name="player" type="CharacterBody3D" parent="." node_paths=PackedStringArray("camera")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -7.76396, 0)
script = ExtResource("2_f4o04")
camera = NodePath("../Camera3D")
[node name="CSGSphere3D" type="CSGSphere3D" parent="player"]
radius = 2.0
@ -46,3 +45,10 @@ material = SubResource("StandardMaterial3D_t1cxb")
[node name="CollisionShape3D" type="CollisionShape3D" parent="player"]
transform = Transform3D(4, 0, 0, 0, 4, 0, 0, 0, 4, 0, 0, 0)
shape = SubResource("SphereShape3D_sjmr4")
[node name="Camera3D" type="Camera3D" parent="." node_paths=PackedStringArray("player")]
transform = Transform3D(1, 0, 0, 0, 0.951057, 0.309017, 0, -0.309017, 0.951057, 0, 4.64082, 18.3615)
environment = SubResource("Environment_srrj6")
current = true
script = ExtResource("3_as6nl")
player = NodePath("../player")

View File

@ -1,8 +1,9 @@
extends CharacterBody3D
@export var camera : Camera3D;
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const SPEED = 20
const JUMP_VELOCITY = 9
# Get the gravity from the project settings to be synced with RigidDynamicBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
@ -20,12 +21,22 @@ func _physics_process(delta):
# 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
# calculate the direction relative to the camera
var camera_dir = camera.get_global_transform().basis.z
camera_dir.y = 0
camera_dir = camera_dir.normalized()
var move_dir = camera_dir * input_dir.y + camera.get_global_transform().basis.x * input_dir.x
move_dir.y = 0
# calculate the speed based on the input direction
if move_dir:
velocity.x = move_dir.x * SPEED
velocity.z = move_dir.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
velocity.x = move_toward(velocity.x, 0, 1)
velocity.z = move_toward(velocity.z, 0, 1)
move_and_slide()

25
puzzle/player_camera.gd Normal file
View File

@ -0,0 +1,25 @@
extends Camera3D
# referene to the player
@export var player : Node3D
@export var player_distance : float = 30
@export var camera_height: float = 10
@export var camera_speed: float = 10
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
delta
# process position
var diff = player.position - self.position + Vector3(0, camera_height, 0)
# if the player is too far away, move the camera
if diff.length() > player_distance:
self.position += diff.normalized() * move_toward(0, diff.length() - player_distance, camera_speed) * delta
look_at(player.position)