tried camera adjusted movement
This commit is contained in:
@@ -6,21 +6,29 @@ public partial class player : CharacterBody3D
|
||||
[Export] public const float Speed = 5.0f;
|
||||
[Export] public const float JumpVelocity = 4.5f;
|
||||
[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
|
||||
//camera global position will be included when calculating the player movement
|
||||
public override void _Ready()
|
||||
{
|
||||
camCenter = GetNode<Marker3D>("camera_center");
|
||||
}
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
allDelta = (float)delta;
|
||||
Vector3 velocity = Velocity;
|
||||
|
||||
if (!IsOnFloor())
|
||||
velocity.y -= gravity * (float)delta;
|
||||
velocity.y -= gravity * allDelta;
|
||||
|
||||
if (Input.IsActionJustPressed("move_jump") && IsOnFloor())
|
||||
velocity.y = JumpVelocity;
|
||||
|
||||
Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward");
|
||||
Vector3 direction = (Transform.basis * new Vector3(inputDir.x, 0, inputDir.y)).Normalized();
|
||||
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") * camRotVector;
|
||||
Vector3 direction = new Vector3(inputDir.x * camRotVector.x, 0, inputDir.y * camRotVector.y);
|
||||
if (direction != Vector3.Zero)
|
||||
{
|
||||
velocity.x = direction.x * Speed;
|
||||
@@ -35,4 +43,16 @@ public partial class player : CharacterBody3D
|
||||
Velocity = velocity;
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user