Full code can be found at: https://tbookout@bitbucket.org/tbookout/squashy-toad.git
Commit = 66 - Cleaned up
One difference is how I clamped velocity, as it is more efficient.
limitForwardSpeedSqr = ForwardLimit ^2
my_Rigidbody.velocity.sqrMagnitude < limitForwardSpeedSqr
To limit “Air Hopping”, I increment a counter when upon jump, and decrement when collider hits the ground.
This way I do not have to offset how high Vive headset is from ground.
public void Jump()
{
//Debug.DrawRay(transform.position, (Vector3.down * distToGroundChk), Color.red);
//Prevent double-jumps+ by setting maxJumps to 1
// Equivalent to: rigidbody.velocity.magnitude < maxForwardSpeed, but faster.
if ((JumpCount < maxJumps) && (my_Rigidbody.velocity.sqrMagnitude < limitForwardSpeedSqr))
{
var jumpVector = (Vector3.ProjectOnPlane(m_Camera.transform.forward, Vector3.up).normalized * jumpStrength);
jumpVector = Vector3.RotateTowards(jumpVector, Vector3.up, (jumpAngle * Mathf.Deg2Rad), 0.0F);
my_Rigidbody.AddForce(jumpVector, ForceMode.VelocityChange);
JumpCount++;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
JumpCount = 0;
}
}