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

@@ -25,30 +25,26 @@ script = ExtResource("1_sqkid")
transform = Transform3D(1, 0, 0, 0, 1.2, 0, 0, 0, 1, 0, 1.2, 0)
shape = SubResource("CapsuleShape3D_bfc1o")
[node name="mesh" type="MeshInstance3D" parent="collision"]
[node name="body" type="MeshInstance3D" parent="collision"]
transform = Transform3D(0.999994, 0, 0.00349065, 0, 1, 0, -0.00349065, 0, 0.999994, 0, 0, 0)
mesh = SubResource("CapsuleMesh_4c62m")
skeleton = NodePath("")
[node name="handright" type="MeshInstance3D" parent="collision/mesh"]
[node name="handright" type="MeshInstance3D" parent="collision/body"]
transform = Transform3D(0.399998, -1.77635e-15, -0.00139626, 4.57356e-16, 0.333333, 0, 0.00139626, -6.20064e-18, 0.399998, 0.699996, 0.25, 0.00244345)
mesh = SubResource("SphereMesh_72sci")
skeleton = NodePath("")
[node name="handleft" type="MeshInstance3D" parent="collision/mesh"]
[node name="handleft" type="MeshInstance3D" parent="collision/body"]
transform = Transform3D(0.399998, -1.77635e-15, -0.00139626, 4.57356e-16, 0.333333, 0, 0.00139626, -6.20064e-18, 0.399998, -0.699996, 0.25, -0.00244345)
mesh = SubResource("SphereMesh_72sci")
skeleton = NodePath("")
[node name="debugfacemesh" type="MeshInstance3D" parent="collision/mesh"]
[node name="debugfacemesh" type="MeshInstance3D" parent="collision/body"]
transform = Transform3D(0.0010964, 0.999994, -0.00331398, -0.791156, -3.45825e-08, -0.261757, -0.314106, 0.00349064, 0.949381, 0.0915702, 0.666667, -0.449683)
mesh = SubResource("TextMesh_fc7r6")
skeleton = NodePath("../../..")
[node name="raycast" type="RayCast3D" parent="collision/mesh"]
transform = Transform3D(-0.999994, 0.00349056, -4.37111e-08, 3.64262e-08, 1.59224e-15, -0.833333, -0.00349056, -0.999994, -1.52579e-10, 0.00174532, -0.333333, -0.499997)
target_position = Vector3(0, 1, 0)
[node name="camera_center" type="Marker3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 0.999999, 0, 1.2, 0)

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