big performance improvement

This commit is contained in:
2023-01-24 13:41:25 +01:00
parent 983dbf978f
commit 17b2fd0817
5 changed files with 38 additions and 36 deletions

View File

@@ -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<MeshInstance3D>("collision/body").Rotation;
bodyRotation.y = Mathf.LerpAngle(bodyRotation.y,Mathf.Atan2(-direction.x, -direction.z), (float)delta * speed);
GetNode<MeshInstance3D>("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<Marker3D>("camera_center").Rotation.y).Normalized(); //rotates the input direction with camera rotation
direction = new Vector3(inputDir.x, 0, inputDir.y).Rotated(Vector3.Up, GetNode<Marker3D>("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<MeshInstance3D>("collision/body").Rotation;
bodyRotation.y = Mathf.LerpAngle(bodyRotation.y,Mathf.Atan2(-direction.x, -direction.z), (float)delta * speed);
GetNode<MeshInstance3D>("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<Marker3D>("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<Marker3D>("camera_center").RotationDegrees = camRot;
camRot.x = Mathf.Clamp(camRot.x, minMousePitch, maxMousePitch); //prevents camera from going endlessly around the player
GetNode<Marker3D>("camera_center").RotationDegrees = camRot;
}
}
}

View File

@@ -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);
}
}
}