Skip to content

Commit

Permalink
Add Quake like camera tilt
Browse files Browse the repository at this point in the history
  • Loading branch information
Steveplays28 committed Jun 21, 2022
1 parent be87169 commit 946f4e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion prefabs/gun1.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ nodes/reload_animation/node = SubResource( 19 )
nodes/reload_animation/position = Vector2( -200, 180 )
nodes/reload_one_shot/node = SubResource( 20 )
nodes/reload_one_shot/position = Vector2( 380, 100 )
node_connections = [ "output", 0, "reload_one_shot", "TimeScale", 0, "reload_animation", "reload_one_shot", 0, "idle_animation", "reload_one_shot", 1, "TimeScale" ]
node_connections = [ "output", 0, "reload_one_shot", "reload_one_shot", 0, "idle_animation", "reload_one_shot", 1, "TimeScale", "TimeScale", 0, "reload_animation" ]

[node name="Gun1" instance=ExtResource( 1 )]
transform = Transform( 0.05, 0, 0, 0, 0.05, 0, 0, 0, 0.05, 0, 0, 0 )
Expand Down
2 changes: 1 addition & 1 deletion scenes/warehouse.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ transform = Transform( 1, 0, 0, 0, -0.965926, 0.258819, 0, -0.258819, -0.965926,
shadow_enabled = true

[node name="Player" parent="." instance=ExtResource( 6 )]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 )
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0 )

[node name="ConcreteFloor1" parent="." instance=ExtResource( 8 )]
transform = Transform( 50, 0, 0, 0, 1, 0, 0, 0, 50, 0, 0, 0 )
Expand Down
40 changes: 28 additions & 12 deletions scripts/PlayerControllerKinematic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ public bool IsGrounded()
return floorRayCast.IsColliding();
}

public Vector3 GetLocalVelocity()
{
return Velocity.Rotated(Vector3.Up, Rotation.y);
}

private void HandleMouseCursorVisibilityInput()
{
if (Input.IsActionJustPressed("toggle_mouse_cursor_visibility"))
Expand Down Expand Up @@ -112,7 +117,18 @@ private void HandleGravity(float delta)

private void HandleMovementInput(float delta)
{
float maxMovementSpeed;
if (Input.IsActionPressed("sprint"))
{
maxMovementSpeed = MaxSprintMovementSpeed;
}
else
{
maxMovementSpeed = MaxMovementSpeed;
}

Vector3 inputDirection = Vector3.Zero;
Vector3 cameraRotationDegrees = camera.RotationDegrees;
if (Input.IsActionPressed("move_forward"))
{
inputDirection -= Transform.basis.z;
Expand All @@ -124,24 +140,19 @@ private void HandleMovementInput(float delta)
if (Input.IsActionPressed("move_right"))
{
inputDirection += Transform.basis.x;

cameraRotationDegrees.z = Mathf.Clamp(cameraRotationDegrees.z - 0.1f * maxMovementSpeed, -2f, 2f);
}
if (Input.IsActionPressed("move_left"))
{
inputDirection -= Transform.basis.x;

cameraRotationDegrees.z = Mathf.Clamp(cameraRotationDegrees.z + 0.1f * maxMovementSpeed, -2f, 2f);
}
inputDirection = inputDirection.Normalized();
camera.RotationDegrees = cameraRotationDegrees;

float maxMovementSpeed;
if (Input.IsActionPressed("sprint"))
{
targetVelocity += inputDirection * MaxSprintMovementSpeed;
maxMovementSpeed = MaxSprintMovementSpeed;
}
else
{
targetVelocity += inputDirection * MaxMovementSpeed;
maxMovementSpeed = MaxMovementSpeed;
}
targetVelocity += inputDirection * maxMovementSpeed;

float decceleration = IsGrounded() ? Decceleration : 0.5f;
if (inputDirection.x == 0f)
Expand Down Expand Up @@ -202,7 +213,7 @@ private void ApplyVelocity(float delta)
if (Velocity.Abs().Length() > 0.1f)
{
animationTree.Set("parameters/idle_walk_blend/blend_amount", Mathf.Clamp(idle_walk_blend_amount + delta, 0f, 1f));
animationTree.Set("parameters/time_scale/scale", Velocity.Rotated(Vector3.Up, Rotation.y).Length() / MaxMovementSpeed / 2f);
animationTree.Set("parameters/time_scale/scale", GetLocalVelocity().Length() / MaxMovementSpeed / 2f);
}
else
{
Expand All @@ -211,6 +222,11 @@ private void ApplyVelocity(float delta)
float time_scale = (float)animationTree.Get("parameters/time_scale/scale");
animationTree.Set("parameters/time_scale/scale", Mathf.Clamp(time_scale + delta, 0f, 1f));
}

if (GetLocalVelocity().Abs().x < 1f)
{
camera.RotationDegrees = new Vector3(camera.RotationDegrees.x, camera.RotationDegrees.y, 0f);
}
}
}
}

0 comments on commit 946f4e3

Please sign in to comment.