added mesh rotation by movement direction

This commit is contained in:
2023-01-02 18:39:26 +01:00
parent ccf9860e91
commit b36007a0b6
2 changed files with 15 additions and 23 deletions

View File

@@ -3,22 +3,15 @@ using System;
public partial class player : CharacterBody3D
{
[Export] public const float Speed = 5.0f;
[Export] public const float JumpVelocity = 5f;
[Export] public const float gravity = 14f;
[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 = -90f;
[Export(PropertyHint.Range, "0,90,1")] float maxMousePitch = 50f;
public float allDelta;
public Marker3D camCenter;
public override void _Ready()
{
camCenter = GetNode<Marker3D>("camera_center");
}
public override void _Process(double delta)
{
allDelta = (float)delta;
if (Input.IsActionJustPressed("uncapture_mouse")) Input.MouseMode = Input.MouseModeEnum.Visible;
if (Input.IsMouseButtonPressed(MouseButton.Left)) Input.MouseMode = Input.MouseModeEnum.Captured;
}
@@ -27,17 +20,20 @@ public partial class player : CharacterBody3D
Vector3 velocity = Velocity;
if (!IsOnFloor())
velocity.y -= gravity * allDelta;
velocity.y -= gravity * (float)delta;
if (Input.IsActionJustPressed("move_jump") && IsOnFloor() && Input.MouseMode == Input.MouseModeEnum.Captured)
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, camCenter.Rotation.y).Normalized();
Vector3 direction = new Vector3(inputDir.x, 0, inputDir.y).Rotated(Vector3.Up, GetNode<Marker3D>("camera_center").Rotation.y).Normalized();
if (direction != Vector3.Zero)
{
velocity.x = direction.x * Speed;
velocity.z = direction.z * Speed;
Vector3 bodyRotation = GetNode<MeshInstance3D>("collision/body").Rotation;
bodyRotation.y = Mathf.LerpAngle(bodyRotation.y,Mathf.Atan2(-inputDir.x, -inputDir.y), (float)delta * Speed);
GetNode<MeshInstance3D>("collision/body").Rotation = bodyRotation;
}
else
{
@@ -46,16 +42,16 @@ public partial class player : CharacterBody3D
}
Velocity = velocity;
MoveAndSlide();
}
}
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseMotion mouseMotion && Input.MouseMode == Input.MouseModeEnum.Captured)
{
Vector3 camRot = camCenter.RotationDegrees;
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);
camCenter.RotationDegrees = camRot;
GetNode<Marker3D>("camera_center").RotationDegrees = camRot;
}
}
}