From 17b2fd0817509ea2e8bd9c8eff3421a468cf9541 Mon Sep 17 00:00:00 2001 From: cmod31 Date: Tue, 24 Jan 2023 13:41:25 +0100 Subject: [PATCH] big performance improvement --- gd-mono-thirdpersoncontroller.csproj | 2 +- gd-mono-thirdpersoncontroller.csproj.old | 7 ++++++ project.godot | 9 ++++---- src/player.cs | 28 ++++++++++++++---------- src/window.cs | 28 +++++++----------------- 5 files changed, 38 insertions(+), 36 deletions(-) create mode 100644 gd-mono-thirdpersoncontroller.csproj.old diff --git a/gd-mono-thirdpersoncontroller.csproj b/gd-mono-thirdpersoncontroller.csproj index 2ceb7b3..be17dbd 100644 --- a/gd-mono-thirdpersoncontroller.csproj +++ b/gd-mono-thirdpersoncontroller.csproj @@ -1,4 +1,4 @@ - + net6.0 true diff --git a/gd-mono-thirdpersoncontroller.csproj.old b/gd-mono-thirdpersoncontroller.csproj.old new file mode 100644 index 0000000..2ceb7b3 --- /dev/null +++ b/gd-mono-thirdpersoncontroller.csproj.old @@ -0,0 +1,7 @@ + + + net6.0 + true + gd-mono-thirdpersoncontroller + + \ No newline at end of file diff --git a/project.godot b/project.godot index a657173..ffb17af 100644 --- a/project.godot +++ b/project.godot @@ -29,6 +29,10 @@ window/stretch/aspect="expand" project/assembly_name="gd-mono-thirdpersoncontroller" +[editor] + +export/convert_text_resources_to_binary=true + [input] move_left={ @@ -75,8 +79,5 @@ uncapture_mouse={ [rendering] -lights_and_shadows/directional_shadow/size=6144 -lights_and_shadows/directional_shadow/soft_shadow_filter_quality=5 -anti_aliasing/quality/msaa_3d=2 -anti_aliasing/quality/screen_space_aa=1 +renderer/rendering_method="mobile" occlusion_culling/use_occlusion_culling=true diff --git a/src/player.cs b/src/player.cs index 9ae0f80..59d4132 100644 --- a/src/player.cs +++ b/src/player.cs @@ -2,18 +2,28 @@ using Godot; public partial class player : CharacterBody3D { + public Vector3 direction; [Export] float speed = 5.0f; [Export] float jumpVelocity = 5f; [Export] float gravity = 14f; [Export(PropertyHint.Range, "0.1,1.0")] float mouseSensitivity = 0.3f; [Export(PropertyHint.Range, "-90,0,1")] float minMousePitch = -50f; - [Export(PropertyHint.Range, "0,90,1")] float maxMousePitch = 50f; + [Export(PropertyHint.Range, "0,90,1")] float maxMousePitch = 50f; public override void _Process(double delta) { //uncapturing the mouse disables player movement but still simulates gravity - if (Input.IsActionJustPressed("uncapture_mouse")) Input.MouseMode = Input.MouseModeEnum.Visible; - if (Input.IsMouseButtonPressed(MouseButton.Left)) Input.MouseMode = Input.MouseModeEnum.Captured; + if (Input.IsActionJustPressed("uncapture_mouse")) Input.MouseMode = Input.MouseModeEnum.Visible; + if (Input.IsMouseButtonPressed(MouseButton.Left)) Input.MouseMode = Input.MouseModeEnum.Captured; + + /**body rotation is in regular process because it lags in physicsprocess and is more a animation anyway + maybe rotate extra collisions separately for invisible lag that may occur**/ + if (direction != Vector3.Zero) + { + Vector3 bodyRotation = GetNode("collision/body").Rotation; + bodyRotation.y = Mathf.LerpAngle(bodyRotation.y,Mathf.Atan2(-direction.x, -direction.z), (float)delta * speed); + GetNode("collision/body").Rotation = bodyRotation; + } } public override void _PhysicsProcess(double delta) { @@ -26,15 +36,11 @@ public partial class player : CharacterBody3D velocity.y = jumpVelocity; Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward"); - Vector3 direction = new Vector3(inputDir.x, 0, inputDir.y).Rotated(Vector3.Up, GetNode("camera_center").Rotation.y).Normalized(); //rotates the input direction with camera rotation + direction = new Vector3(inputDir.x, 0, inputDir.y).Rotated(Vector3.Up, GetNode("camera_center").Rotation.y).Normalized(); //rotates the input direction with camera rotation if (direction != Vector3.Zero) { velocity.x = direction.x * speed; velocity.z = direction.z * speed; - //Rotating the body mesh to movement - Vector3 bodyRotation = GetNode("collision/body").Rotation; - bodyRotation.y = Mathf.LerpAngle(bodyRotation.y,Mathf.Atan2(-direction.x, -direction.z), (float)delta * speed); - GetNode("collision/body").Rotation = bodyRotation; } else { @@ -43,7 +49,7 @@ public partial class player : CharacterBody3D } Velocity = velocity; MoveAndSlide(); - } + } public override void _Input(InputEvent @event) { if (@event is InputEventMouseMotion mouseMotion && Input.MouseMode == Input.MouseModeEnum.Captured) @@ -51,8 +57,8 @@ public partial class player : CharacterBody3D Vector3 camRot = GetNode("camera_center").RotationDegrees; camRot.y -= mouseMotion.Relative.x * mouseSensitivity; camRot.x -= mouseMotion.Relative.y * mouseSensitivity; - camRot.x = Mathf.Clamp(camRot.x, minMousePitch, maxMousePitch); //prevents camera from going endlessly around the player - GetNode("camera_center").RotationDegrees = camRot; + camRot.x = Mathf.Clamp(camRot.x, minMousePitch, maxMousePitch); //prevents camera from going endlessly around the player + GetNode("camera_center").RotationDegrees = camRot; } } } diff --git a/src/window.cs b/src/window.cs index 46648b8..8289d3f 100644 --- a/src/window.cs +++ b/src/window.cs @@ -3,25 +3,13 @@ using System; public partial class window : Node { - public override async void _Ready() - { - /* VSync should be used because needing to change the PhysicsTicksPerSecond in _Process takes CPU ussage - * You could also just set max FPS and delte this fix - */ - await ToSignal(GetTree().CreateTimer(1.5f), "timeout");//waits until the game has loaded some time - if (Engine.PhysicsTicksPerSecond != (int)Engine.GetFramesPerSecond()) - { - Engine.PhysicsTicksPerSecond = (int)Engine.GetFramesPerSecond(); //PhysicsTicksPerSecond have to be the same value like current FPS or the movement will lagg - GD.Print("Set PhysicsTicksPerSecond to: " + Engine.PhysicsTicksPerSecond); - } - } - public override void _Process(double delta) + public override void _Process(double delta) { - if (Input.IsActionJustPressed("fullscreen")) - { - if (DisplayServer.WindowGetMode() == DisplayServer.WindowMode.Fullscreen) - DisplayServer.WindowSetMode(DisplayServer.WindowMode.Windowed); - else DisplayServer.WindowSetMode(DisplayServer.WindowMode.Fullscreen); - } - } + if (Input.IsActionJustPressed("fullscreen")) + { + if (DisplayServer.WindowGetMode() == DisplayServer.WindowMode.Fullscreen) + DisplayServer.WindowSetMode(DisplayServer.WindowMode.Windowed); + else DisplayServer.WindowSetMode(DisplayServer.WindowMode.Fullscreen); + } + } }