Skip to content

Commit

Permalink
Merge pull request #256 from hackerspace-ntnu/feature/slope-movement
Browse files Browse the repository at this point in the history
Improve slope movement
  • Loading branch information
toberge committed Oct 21, 2023
2 parents 528d974 + 2e76c41 commit 9342d87
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions Assets/Prefabs/Input/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ MonoBehaviour:
leapTimeout: 0.875
crouchedHeightOffset: 0.3
airThreshold: 0.4
slopeAngleThreshold: 50
state: 1
animator: {fileID: 289053663840324231}
--- !u!23 &71015680234268065
Expand Down
26 changes: 23 additions & 3 deletions Assets/Scripts/Control&Input/PlayerMovement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class PlayerMovement : MonoBehaviour
[SerializeField]
private float leapTimeout = 0.875f;

private const float marsGravity = 3.72076f;
private const float MarsGravity = 3.72076f;

private bool canJump = true;

Expand All @@ -67,6 +67,9 @@ public class PlayerMovement : MonoBehaviour
[SerializeField]
private float airThreshold = 0.4f;

[SerializeField]
private float slopeAngleThreshold = 50;

[SerializeField, ReadOnly]
private PlayerState state = PlayerState.GROUNDED;

Expand Down Expand Up @@ -158,6 +161,20 @@ private bool IsInAir()
return !Physics.BoxCast(hitbox.bounds.center, 0.5f * Vector3.one, Vector3.down, Quaternion.identity, 0.5f + airThreshold, ignoreMask); ;
}

private Vector3 GroundNormal()
{
// Cast a box to detect (partial) ground. See OnDrawGizmos for what I think is the extent of the box cast.
// No, this does not work if the cast start at the bottom.
if (Physics.BoxCast(hitbox.bounds.center, 0.5f * Vector3.one, Vector3.down, out var hit, Quaternion.identity,
0.5f + airThreshold, ignoreMask))
{
var angle = Vector3.Angle(hit.normal, Vector3.up);
var isAngleWithinThreshold = angle < slopeAngleThreshold && angle > 0;
return isAngleWithinThreshold ? hit.normal : Vector3.up;
}
return Vector3.up;
}

private void UpdatePosition(Vector3 input)
{
// Modify input to addforce with relation to current rotation.
Expand All @@ -169,15 +186,18 @@ private void UpdatePosition(Vector3 input)
// Strafe slightly with less drag.
body.drag = airDrag;
body.AddForce(input * strafeForceInAir, ForceMode.VelocityChange);
body.AddForce(Vector3.down * marsGravity, ForceMode.Acceleration);
body.AddForce(Vector3.down * MarsGravity, ForceMode.Acceleration);
if (!IsInAir()) state = PlayerState.GROUNDED;
break;
}
case PlayerState.GROUNDED:
{
// Strafe normally with heavy drag.
body.drag = drag;
body.AddForce(input * strafeForce, ForceMode.VelocityChange);
// Walk along ground normal (adjusted if on heavy slope).
var groundNormal = GroundNormal();
var direction = Vector3.ProjectOnPlane(input, groundNormal);
body.AddForce(direction * strafeForce, ForceMode.VelocityChange);
if (IsInAir()) state = PlayerState.IN_AIR;
break;
}
Expand Down

0 comments on commit 9342d87

Please sign in to comment.