started rewriting player controller
This commit is contained in:
@@ -69,6 +69,10 @@ uncapture_mouse={
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[physics]
|
||||||
|
|
||||||
|
3d/default_gravity=12.0
|
||||||
|
|
||||||
[rendering]
|
[rendering]
|
||||||
|
|
||||||
anti_aliasing/quality/msaa_3d=2
|
anti_aliasing/quality/msaa_3d=2
|
||||||
|
@@ -71,3 +71,8 @@ transform = Transform3D(0.994703, -0.00611287, -0.102607, 0.101106, 0.238088, 0.
|
|||||||
shadow_enabled = true
|
shadow_enabled = true
|
||||||
|
|
||||||
[node name="player" parent="." instance=ExtResource("1_1hi18")]
|
[node name="player" parent="." instance=ExtResource("1_1hi18")]
|
||||||
|
JumpVelocity = null
|
||||||
|
gravity = null
|
||||||
|
mouseSensitivity = null
|
||||||
|
minCamPitch = null
|
||||||
|
maxCamPitch = null
|
||||||
|
@@ -53,10 +53,10 @@ shape = SubResource("CapsuleShape3D_bfc1o")
|
|||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 0.999999, 0, 1.2, 0)
|
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 0.999999, 0, 1.2, 0)
|
||||||
|
|
||||||
[node name="spring_arm" type="SpringArm3D" parent="camera_center"]
|
[node name="spring_arm" type="SpringArm3D" parent="camera_center"]
|
||||||
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, -5.96046e-08, 1, 0, 0, -0.5)
|
transform = Transform3D(1, 0, 0, 0, 0.992593, 0.121488, 0, -0.121488, 0.992593, 0, 0.167049, -0.637988)
|
||||||
shape = SubResource("SeparationRayShape3D_31c1r")
|
shape = SubResource("SeparationRayShape3D_31c1r")
|
||||||
spring_length = 6.0
|
spring_length = 6.0
|
||||||
|
|
||||||
[node name="camera" type="Camera3D" parent="camera_center/spring_arm"]
|
[node name="camera" type="Camera3D" parent="camera_center/spring_arm"]
|
||||||
transform = Transform3D(1, 1.54914e-09, -6.87955e-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
|
||||||
|
@@ -3,79 +3,36 @@ using System;
|
|||||||
|
|
||||||
public partial class player : CharacterBody3D
|
public partial class player : CharacterBody3D
|
||||||
{
|
{
|
||||||
public float gotDelta;
|
[Export] public const float Speed = 5.0f;
|
||||||
public const float Speed = 5.0f;
|
[Export] public const float JumpVelocity = 4.5f;
|
||||||
[Export] float JumpVelocity = 5.5f;
|
[Export] public const float gravity = 9f;
|
||||||
[Export] float gravity = 14f;
|
|
||||||
[Export (PropertyHint.Range, "0.1,1.0")] float mouseSensitivity = 0.3f;
|
//eveyrthing will be replaced with roatation and direction based movement
|
||||||
[Export (PropertyHint.Range, "-90,0,1")] float minCamPitch = -90f;
|
//camera global position will be included when calculating the player movement
|
||||||
[Export (PropertyHint.Range, "0,90,1")] float maxCamPitch = 90f;
|
|
||||||
public Marker3D cameraCenter;
|
|
||||||
public Camera3D camera;
|
|
||||||
public Vector3 camPosition;
|
|
||||||
public Vector3 playerResetPosition;
|
|
||||||
//TODO: Add camera max rotation on ground (Preventing turning camera to upside down)
|
|
||||||
//TODO: just rotate model not just everything
|
|
||||||
//NOTICE: Since Beta 7 of Godot 4 the turning isn't smooth anymore
|
|
||||||
public override void _Ready()
|
|
||||||
{
|
|
||||||
cameraCenter = GetNode<Marker3D>("camera_center");
|
|
||||||
camera = GetNode<Camera3D>("camera_center/spring_arm/camera");
|
|
||||||
mouseSensitivity = mouseSensitivity / 2;
|
|
||||||
minCamPitch = minCamPitch + (float)Math.PI / 2;
|
|
||||||
maxCamPitch = maxCamPitch + (float)Math.PI / 2;
|
|
||||||
Input.MouseMode = Input.MouseModeEnum.Captured;
|
|
||||||
}
|
|
||||||
public override void _Process(double delta)
|
|
||||||
{
|
|
||||||
gotDelta = (float)delta;
|
|
||||||
if (Input.IsActionJustPressed("uncapture_mouse")) Input.MouseMode = Input.MouseModeEnum.Visible;
|
|
||||||
if (Input.IsMouseButtonPressed(MouseButton.Left)) Input.MouseMode = Input.MouseModeEnum.Captured;//just uncapture to also disable player and camera movement
|
|
||||||
}
|
|
||||||
public override void _PhysicsProcess(double delta)
|
public override void _PhysicsProcess(double delta)
|
||||||
{
|
{
|
||||||
Vector3 velocity = Velocity;
|
Vector3 velocity = Velocity;
|
||||||
|
|
||||||
if (!IsOnFloor())
|
if (!IsOnFloor())
|
||||||
velocity.y -= gravity * (float)delta;
|
velocity.y -= gravity * (float)delta;
|
||||||
|
|
||||||
if (Input.IsActionJustPressed("move_jump" ) && Input.MouseMode == Input.MouseModeEnum.Captured && IsOnFloor())
|
if (Input.IsActionJustPressed("move_jump") && IsOnFloor())
|
||||||
velocity.y = JumpVelocity;
|
velocity.y = JumpVelocity;
|
||||||
|
|
||||||
Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward");
|
Vector2 inputDir = Input.GetVector("move_left", "move_right", "move_forward", "move_backward");
|
||||||
Vector3 direction = (Transform.basis * new Vector3(inputDir.x, 0, inputDir.y)).Normalized();
|
Vector3 direction = (Transform.basis * new Vector3(inputDir.x, 0, inputDir.y)).Normalized();
|
||||||
if (direction != Vector3.Zero && Input.MouseMode == Input.MouseModeEnum.Captured)
|
if (direction != Vector3.Zero)
|
||||||
{
|
{
|
||||||
velocity.x = direction.x * Speed;
|
velocity.x = direction.x * Speed;
|
||||||
velocity.z = direction.z * Speed;
|
velocity.z = direction.z * Speed;
|
||||||
|
}
|
||||||
//checks for movement and adjusts the character direction with camera roatation
|
|
||||||
playerResetPosition = Rotation;
|
|
||||||
camPosition = cameraCenter.Rotation;
|
|
||||||
playerResetPosition.y = camPosition.y * -1;
|
|
||||||
Rotation -= playerResetPosition;
|
|
||||||
cameraCenter.Rotation = new Vector3(camPosition.x, 0, 0);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
velocity.x = Mathf.MoveToward(Velocity.x, 0, Speed);
|
velocity.x = Mathf.MoveToward(Velocity.x, 0, Speed);
|
||||||
velocity.z = Mathf.MoveToward(Velocity.z, 0, Speed);
|
velocity.z = Mathf.MoveToward(Velocity.z, 0, Speed);
|
||||||
}
|
}
|
||||||
|
|
||||||
Velocity = velocity;
|
Velocity = velocity;
|
||||||
|
|
||||||
MoveAndSlide();
|
MoveAndSlide();
|
||||||
}
|
}
|
||||||
public override void _Input(InputEvent @event)
|
|
||||||
{
|
|
||||||
if(@event is InputEventMouseMotion motionEvent && Input.MouseMode == Input.MouseModeEnum.Captured)
|
|
||||||
{
|
|
||||||
Vector3 generalRot = cameraCenter.Rotation;
|
|
||||||
generalRot.y -= motionEvent.Relative.x * mouseSensitivity * (float)gotDelta;
|
|
||||||
generalRot.x -= motionEvent.Relative.y * mouseSensitivity * (float)gotDelta;
|
|
||||||
//generalRot.x = Mathf.Clamp(generalRot.x, minCamPitch, maxCamPitch); // doesn't do anything?
|
|
||||||
cameraCenter.Rotation = generalRot;
|
|
||||||
generalRot = cameraCenter.Rotation;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user