From 4de70a539b296cdcec375a4bb5f374e35bdb0777 Mon Sep 17 00:00:00 2001 From: Victor Turgeon Date: Tue, 11 Oct 2022 09:26:29 -0400 Subject: [PATCH] Basic character controller --- Materials/Dark_ProtoMat.tres | 7 +++ Materials/Orange_ProtoMat.tres | 7 +++ Player.gd | 54 ++++++++++++++++++- Player.tscn | 5 ++ Test_Scene.tscn | 22 ++++---- .../orange/texture_01.png.import | 18 ++++--- project.godot | 33 ++++++++++++ 7 files changed, 128 insertions(+), 18 deletions(-) create mode 100644 Materials/Dark_ProtoMat.tres create mode 100644 Materials/Orange_ProtoMat.tres diff --git a/Materials/Dark_ProtoMat.tres b/Materials/Dark_ProtoMat.tres new file mode 100644 index 0000000..afa90bd --- /dev/null +++ b/Materials/Dark_ProtoMat.tres @@ -0,0 +1,7 @@ +[gd_resource type="SpatialMaterial" load_steps=2 format=2] + +[ext_resource path="res://Textures/kenney_prototype_textures/dark/texture_01.png" type="Texture" id=1] + +[resource] +albedo_texture = ExtResource( 1 ) +uv1_triplanar = true diff --git a/Materials/Orange_ProtoMat.tres b/Materials/Orange_ProtoMat.tres new file mode 100644 index 0000000..5fbc39f --- /dev/null +++ b/Materials/Orange_ProtoMat.tres @@ -0,0 +1,7 @@ +[gd_resource type="SpatialMaterial" load_steps=2 format=2] + +[ext_resource path="res://Textures/kenney_prototype_textures/orange/texture_01.png" type="Texture" id=1] + +[resource] +albedo_texture = ExtResource( 1 ) +uv1_triplanar = true diff --git a/Player.gd b/Player.gd index a39fdf5..ffbba56 100644 --- a/Player.gd +++ b/Player.gd @@ -1,8 +1,20 @@ extends KinematicBody -var mouse_sensitivity = 0.03 +export var speed = 20 +export var h_acceleration = 6 +export var gravity = 40 +export var jump = 30 + +export var mouse_sensitivity = 0.03 + +var full_contact = false +var direction = Vector3() +var h_velocity = Vector3() +var movement = Vector3() +var gravity_vec = Vector3() onready var head = $Head +onready var ground_check = $GroundCheck func _ready(): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) @@ -12,3 +24,43 @@ func _input(event): rotate_y(deg2rad(-event.relative.x * mouse_sensitivity)) head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity)) head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89)) + + +func _physics_process(delta): + + if Input.is_action_just_pressed("uncapture_mouse"): + Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) + elif Input.is_action_just_released("uncapture_mouse"): + Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) + + direction = Vector3() + + full_contact = ground_check.is_colliding() + + if not is_on_floor(): + gravity_vec += Vector3.DOWN * gravity * delta + elif is_on_floor() and full_contact: + gravity_vec = -get_floor_normal() * gravity + else: + gravity_vec = -get_floor_normal() + + + if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()): + gravity_vec = Vector3.UP * jump + + if Input.is_action_pressed("move_forward"): + direction -= transform.basis.z + elif Input.is_action_pressed("move_backward"): + direction += transform.basis.z + if Input.is_action_pressed("move_left"): + direction -= transform.basis.x + elif Input.is_action_pressed("move_right"): + direction += transform.basis.x + + direction = direction.normalized() + h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta) + movement.z = h_velocity.z + gravity_vec.z + movement.x = h_velocity.x + gravity_vec.x + movement.y = gravity_vec.y + + move_and_slide(movement, Vector3.UP) diff --git a/Player.tscn b/Player.tscn index 9c6ebf1..106ed57 100644 --- a/Player.tscn +++ b/Player.tscn @@ -29,3 +29,8 @@ shape = SubResource( 3 ) transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.84218, 0 ) [node name="Camera" type="Camera" parent="Head"] + +[node name="GroundCheck" type="RayCast" parent="."] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -2, 0 ) +enabled = true +cast_to = Vector3( 0, -1.5, 0 ) diff --git a/Test_Scene.tscn b/Test_Scene.tscn index 6583cb4..ee3051c 100644 --- a/Test_Scene.tscn +++ b/Test_Scene.tscn @@ -1,23 +1,27 @@ [gd_scene load_steps=4 format=2] -[ext_resource path="res://Textures/kenney_prototype_textures/dark/texture_01.png" type="Texture" id=1] +[ext_resource path="res://Materials/Dark_ProtoMat.tres" type="Material" id=1] [ext_resource path="res://Player.tscn" type="PackedScene" id=2] - -[sub_resource type="SpatialMaterial" id=1] -albedo_texture = ExtResource( 1 ) -uv1_triplanar = true +[ext_resource path="res://Materials/Orange_ProtoMat.tres" type="Material" id=3] [node name="Spatial" type="Spatial"] [node name="CSGCombiner" type="CSGCombiner" parent="."] +use_collision = true [node name="CSGBox" type="CSGBox" parent="CSGCombiner"] -transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -4, 0 ) -width = 26.0 -depth = 26.0 -material = SubResource( 1 ) +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1, 0 ) +width = 106.0 +depth = 82.0 +material = ExtResource( 1 ) + +[node name="CSGPolygon" type="CSGPolygon" parent="CSGCombiner"] +transform = Transform( 3.51681, 0, 0, 0, 4.16599, 0, 0, 0, 14.5504, 22.4026, 0, 7 ) +polygon = PoolVector2Array( 0, 0, -3, 0, 1, 1, 1, 0 ) +material = ExtResource( 3 ) [node name="DirectionalLight" type="DirectionalLight" parent="."] transform = Transform( 1, 0, 0, 0, 0.258819, 0.965926, 0, -0.965926, 0.258819, 0, 15.4487, 13 ) [node name="Player" parent="." instance=ExtResource( 2 )] +transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 9, 0 ) diff --git a/Textures/kenney_prototype_textures/orange/texture_01.png.import b/Textures/kenney_prototype_textures/orange/texture_01.png.import index 928e058..067b113 100644 --- a/Textures/kenney_prototype_textures/orange/texture_01.png.import +++ b/Textures/kenney_prototype_textures/orange/texture_01.png.import @@ -2,28 +2,30 @@ importer="texture" type="StreamTexture" -path="res://.import/texture_01.png-7bf1c5b580a4e5291a946c3b0f1b0019.stex" +path.s3tc="res://.import/texture_01.png-7bf1c5b580a4e5291a946c3b0f1b0019.s3tc.stex" +path.etc2="res://.import/texture_01.png-7bf1c5b580a4e5291a946c3b0f1b0019.etc2.stex" metadata={ -"vram_texture": false +"imported_formats": [ "s3tc", "etc2" ], +"vram_texture": true } [deps] source_file="res://Textures/kenney_prototype_textures/orange/texture_01.png" -dest_files=[ "res://.import/texture_01.png-7bf1c5b580a4e5291a946c3b0f1b0019.stex" ] +dest_files=[ "res://.import/texture_01.png-7bf1c5b580a4e5291a946c3b0f1b0019.s3tc.stex", "res://.import/texture_01.png-7bf1c5b580a4e5291a946c3b0f1b0019.etc2.stex" ] [params] -compress/mode=0 +compress/mode=2 compress/lossy_quality=0.7 compress/hdr_mode=0 compress/bptc_ldr=0 compress/normal_map=0 -flags/repeat=0 +flags/repeat=true flags/filter=true -flags/mipmaps=false +flags/mipmaps=true flags/anisotropic=false -flags/srgb=2 +flags/srgb=1 process/fix_alpha_border=true process/premult_alpha=false process/HDR_as_SRGB=false @@ -31,5 +33,5 @@ process/invert_color=false process/normal_map_invert_y=false stream=false size_limit=0 -detect_3d=true +detect_3d=false svg/scale=1.0 diff --git a/project.godot b/project.godot index df859d8..86ff65e 100644 --- a/project.godot +++ b/project.godot @@ -18,6 +18,39 @@ config/icon="res://icon.png" common/drop_mouse_on_gui_input_disabled=true +[input] + +move_forward={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":87,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} +move_backward={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":83,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} +move_left={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":65,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} +move_right={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":68,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} +jump={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":32,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} +uncapture_mouse={ +"deadzone": 0.5, +"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777240,"physical_scancode":0,"unicode":0,"echo":false,"script":null) + ] +} + [physics] common/enable_pause_aware_picking=true