tried camera adjusted movement

This commit is contained in:
2023-01-01 20:07:23 +01:00
parent d23f2a0a70
commit 1ff156e8a5
2 changed files with 30 additions and 10 deletions

View File

@@ -49,14 +49,14 @@ skeleton = NodePath("../../..")
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) 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) target_position = Vector3(0, 1, 0)
[node name="camera_center" type="Marker3D" parent="collision"] [node name="camera_center" type="Marker3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.833333, 0, 0, 0, 0.999999, 0, 0, 0) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 0.999999, 0, 1.2, 0)
[node name="spring_arm" type="SpringArm3D" parent="collision/camera_center"] [node name="spring_arm" type="SpringArm3D" parent="camera_center"]
transform = Transform3D(1, 0, 0, 0, 0.992593, 0.121488, 0, -0.121488, 0.992593, 0, 0, -0.7) transform = Transform3D(1, 0, 0, 0, 0.992593, 0.121488, 0, -0.121488, 0.992593, 0, 0, -0.7)
shape = SubResource("SeparationRayShape3D_31c1r") shape = SubResource("SeparationRayShape3D_31c1r")
spring_length = 6.0 spring_length = 6.0
[node name="camera" type="Camera3D" parent="collision/camera_center/spring_arm"] [node name="camera" type="Camera3D" parent="camera_center/spring_arm"]
transform = Transform3D(1, 1.54914e-09, -6.87954e-10, -1.62981e-09, 0.990268, -0.139173, 4.65661e-10, 0.139173, 0.990269, 0, 0.0182246, 6.09616) transform = Transform3D(1, 1.54914e-09, -6.87954e-10, -1.62981e-09, 0.990268, -0.139173, 4.65661e-10, 0.139173, 0.990269, 0, 0.0182246, 6.09616)
current = true current = true

View File

@@ -6,21 +6,29 @@ public partial class player : CharacterBody3D
[Export] public const float Speed = 5.0f; [Export] public const float Speed = 5.0f;
[Export] public const float JumpVelocity = 4.5f; [Export] public const float JumpVelocity = 4.5f;
[Export] public const float gravity = 9f; [Export] public const float gravity = 9f;
[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;
//eveyrthing will be replaced with roatation and direction based movement public override void _Ready()
//camera global position will be included when calculating the player movement {
camCenter = GetNode<Marker3D>("camera_center");
}
public override void _PhysicsProcess(double delta) public override void _PhysicsProcess(double delta)
{ {
allDelta = (float)delta;
Vector3 velocity = Velocity; Vector3 velocity = Velocity;
if (!IsOnFloor()) if (!IsOnFloor())
velocity.y -= gravity * (float)delta; velocity.y -= gravity * allDelta;
if (Input.IsActionJustPressed("move_jump") && IsOnFloor()) if (Input.IsActionJustPressed("move_jump") && IsOnFloor())
velocity.y = JumpVelocity; velocity.y = JumpVelocity;
Vector2 camRotVector = new Vector2 ((float)Math.Cos(camCenter.Rotation.y), (float)Math.Sin(camCenter.Rotation.y));
Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward"); Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward") * camRotVector;
Vector3 direction = (Transform.basis * new Vector3(inputDir.x, 0, inputDir.y)).Normalized(); Vector3 direction = new Vector3(inputDir.x * camRotVector.x, 0, inputDir.y * camRotVector.y);
if (direction != Vector3.Zero) if (direction != Vector3.Zero)
{ {
velocity.x = direction.x * Speed; velocity.x = direction.x * Speed;
@@ -35,4 +43,16 @@ public partial class player : CharacterBody3D
Velocity = velocity; Velocity = velocity;
MoveAndSlide(); MoveAndSlide();
} }
public override void _Input(InputEvent @event)
{
if (@event is InputEventMouseMotion mouseMotion)
{
Vector3 camRot = camCenter.RotationDegrees;
camRot.y -= mouseMotion.Relative.x * mouseSensitivity;
camRot.x -= mouseMotion.Relative.y * mouseSensitivity;
camRot.x = Mathf.Clamp(camRot.x, minMousePitch, maxMousePitch);
camCenter.RotationDegrees = camRot;
}
}
} }