Skip to content

Commit

Permalink
Improve player movement
Browse files Browse the repository at this point in the history
  • Loading branch information
vanjac committed Nov 11, 2023
1 parent 7b6eac1 commit 2d6524f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 62 deletions.
111 changes: 68 additions & 43 deletions Assets/Game/NewRigidbodyController.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
using System.Collections;
using System.Linq.Expressions;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;
using UnityStandardAssets.CrossPlatformInput;

public class NewRigidbodyController : MonoBehaviour
{
public float walkSpeed = 3.5f;
public float fallMoveSpeed = 1.5f;
public float jumpForce = 55f;
public float swimForce = 70f;
private const float walkSpeed = 3.5f;
private const float walkAccel = 6.0f;
private const float stopDrag = 3.0f;
private const float waterDrag = 5.0f;
private const float stopDynamicFriction = 1.0f;
private const float stopStaticFriction = 10.0f;
private const float fallMoveSpeed = 1.5f;
private const float jumpVel = 5.5f;
private const float swimVel = 7.0f;
public AnimationCurve slopeCurveModifier = new AnimationCurve(new Keyframe(-90.0f, 1.0f), new Keyframe(0.0f, 1.0f), new Keyframe(90.0f, 0.0f));

public float groundCheckDistance = 0.1f; // distance for checking if the controller is grounded ( 0.01f seems to work best for this )
public float stickToGroundHelperDistance = 0.6f; // stops the character
public float shellOffset = 0.1f; //reduce the radius by that ratio to avoid getting stuck in wall (a value of 0.1f is nice)
public float footstepStride = 1.0f;
private const float groundCheckDistance = 0.15f; // distance for checking if the controller is grounded ( 0.01f seems to work best for this )
private const float stickToGroundHelperDistance = 0.6f; // stops the character
private const float shellOffset = 0.1f; //reduce the radius by that ratio to avoid getting stuck in wall (a value of 0.1f is nice)
private const float footstepStride = 1.0f;

public MouseLook mouseLook = new MouseLook();

public Camera cam;
private Rigidbody rigidBody;
private CapsuleCollider capsule;
private FootstepSounds footstepSoundPlayer;
private float yRotation;
private Vector3 groundContactNormal;
private bool jump, previouslyGrounded, jumping, grounded;
public bool disableGroundCheck;
Expand Down Expand Up @@ -50,48 +55,65 @@ void Update()

void FixedUpdate()
{
GroundCheck();
Vector2 input = new Vector2(
CrossPlatformInputManager.GetAxis("Horizontal"),
CrossPlatformInputManager.GetAxis("Vertical"));
bool hasInput = input.sqrMagnitude > 1e-12;

GroundCheck(hasInput);
bool underWater = false;
PhysicsComponent physicsComponent = GetComponent<PhysicsComponent>();
if (physicsComponent != null)
underWater = physicsComponent.underWater;

Vector2 input = new Vector2
if (underWater)
{
capsule.material.dynamicFriction = 0.0f;
capsule.material.staticFriction = 0.0f;
capsule.material.frictionCombine = PhysicMaterialCombine.Minimum;
rigidBody.drag = waterDrag;
}
else if (grounded && !hasInput && !jump)
{
capsule.material.dynamicFriction = stopDynamicFriction * SlopeMultiplier();
capsule.material.staticFriction = stopStaticFriction * SlopeMultiplier();
capsule.material.frictionCombine = PhysicMaterialCombine.Maximum;
rigidBody.drag = stopDrag * SlopeMultiplier();
}
else
{
x = CrossPlatformInputManager.GetAxis("Horizontal"),
y = CrossPlatformInputManager.GetAxis("Vertical")
};
capsule.material.dynamicFriction = 0.0f;
capsule.material.staticFriction = 0.0f;
capsule.material.frictionCombine = PhysicMaterialCombine.Minimum;
rigidBody.drag = 0;
}

if (Mathf.Abs(input.x) > float.Epsilon || Mathf.Abs(input.y) > float.Epsilon)
float maxSpeed = (grounded && !underWater) ? walkSpeed : fallMoveSpeed;
Vector3 desiredMove = Quaternion.AngleAxis(cam.transform.rotation.eulerAngles.y, Vector3.up)
* new Vector3(input.x, 0, input.y);
desiredMove *= input.magnitude * maxSpeed;
if (hasInput &&
(grounded || underWater || desiredMove.sqrMagnitude > rigidBody.velocity.sqrMagnitude))
{
float maxSpeed = (grounded && !underWater) ? walkSpeed : fallMoveSpeed;
// always move along the camera forward as it is the direction that it being aimed at
Vector3 desiredMove = Quaternion.AngleAxis(cam.transform.rotation.eulerAngles.y, Vector3.up)
* new Vector3(input.x, 0, input.y);
desiredMove = Vector3.ProjectOnPlane(desiredMove, groundContactNormal).normalized;
desiredMove *= input.magnitude * maxSpeed;
if (rigidBody.velocity.sqrMagnitude < (maxSpeed * maxSpeed))
{
// TODO: scale by time??
rigidBody.AddForce(desiredMove * SlopeMultiplier(), ForceMode.Impulse);
}
Vector3 moveVector = desiredMove - rigidBody.velocity;
moveVector = Vector3.ProjectOnPlane(moveVector, groundContactNormal).normalized;
float maxVelChange = walkAccel * maxSpeed * Time.fixedDeltaTime;
if (moveVector.sqrMagnitude > maxVelChange * maxVelChange)
moveVector *= maxVelChange / moveVector.magnitude;
rigidBody.AddForce(moveVector * SlopeMultiplier(), ForceMode.VelocityChange);
}

if (grounded || underWater)
{
rigidBody.drag = 5f;

if (jump)
{
rigidBody.drag = 0f;
rigidBody.velocity = new Vector3(rigidBody.velocity.x, 0f, rigidBody.velocity.z);
rigidBody.AddForce(new Vector3(0f, underWater ? swimForce : jumpForce, 0f), ForceMode.Impulse);
rigidBody.AddForce(new Vector3(0f, underWater ? swimVel : jumpVel, 0f), ForceMode.VelocityChange);
jumping = true;
}
}
else
{
rigidBody.drag = 0f;
if (previouslyGrounded && !jumping)
{
StickToGroundHelper();
Expand Down Expand Up @@ -186,7 +208,7 @@ private void RotateView()
}

/// sphere cast down just beyond the bottom of the capsule to see if the capsule is colliding round the bottom
private void GroundCheck()
private void GroundCheck(bool hasInput)
{
previouslyGrounded = grounded;
if (disableGroundCheck)
Expand All @@ -202,20 +224,23 @@ private void GroundCheck()
{
grounded = true;
groundContactNormal = hitInfo.normal;
// move with moving object
Vector3 move = Vector3.zero;
foreach (IMotionComponent motionComponent in hitInfo.transform.GetComponents<IMotionComponent>())
if (hasInput)
{
if (motionComponent.enabled)
// move with moving object
Vector3 move = Vector3.zero;
foreach (IMotionComponent motionComponent in hitInfo.transform.GetComponents<IMotionComponent>())
{
move += motionComponent.GetTranslateFixed();
Vector3 relPos = transform.position - motionComponent.transform.position;
move += (motionComponent.GetRotateFixed() * relPos) - relPos;
if (motionComponent.enabled)
{
move += motionComponent.GetTranslateFixed();
Vector3 relPos = transform.position - motionComponent.transform.position;
move += (motionComponent.GetRotateFixed() * relPos) - relPos;
}
}
}
move.y = 0;
if (move != Vector3.zero)
rigidBody.MovePosition(rigidBody.position + move);
move.y = 0;
if (move != Vector3.zero)
rigidBody.MovePosition(rigidBody.position + move);
} // else rely on friction

// determine footstep sound
if (hitInfo.collider.gameObject.tag == "Voxel")
Expand Down
21 changes: 2 additions & 19 deletions Assets/Resources/ObjectPrefabs/Player.prefab
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b83727d23b5158d43aecefb4df105983, type: 3}
m_Name:
m_EditorClassIdentifier:
walkSpeed: 3.5
fallMoveSpeed: 1.5
jumpForce: 55
swimForce: 70
slopeCurveModifier:
serializedVersion: 2
m_Curve:
Expand All @@ -186,7 +182,7 @@ MonoBehaviour:
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 0
time: 45
value: 1
inSlope: 0
outSlope: 0
Expand All @@ -195,16 +191,7 @@ MonoBehaviour:
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 63.896103
value: 0.86269474
inSlope: -0.0053077624
outSlope: -0.0053077624
tangentMode: 0
weightedMode: 0
inWeight: 0.33333334
outWeight: 0.33333334
- serializedVersion: 3
time: 90
time: 70
value: 0
inSlope: 0
outSlope: 0
Expand All @@ -215,10 +202,6 @@ MonoBehaviour:
m_PreInfinity: 2
m_PostInfinity: 2
m_RotationOrder: 0
groundCheckDistance: 0.1
stickToGroundHelperDistance: 0.6
shellOffset: 0.1
footstepStride: 1
mouseLook:
XSensitivity: 2
YSensitivity: 2
Expand Down

0 comments on commit 2d6524f

Please sign in to comment.