Jumping
This commit is contained in:
parent
8ce13c35a7
commit
676700ecfa
@ -1001,6 +1001,8 @@ MonoBehaviour:
|
|||||||
m_EditorClassIdentifier:
|
m_EditorClassIdentifier:
|
||||||
maxSpeed: 10
|
maxSpeed: 10
|
||||||
maxAcceleration: 10
|
maxAcceleration: 10
|
||||||
|
jumpHeight: 2
|
||||||
|
maxAirJumps: 1
|
||||||
--- !u!54 &1695436951
|
--- !u!54 &1695436951
|
||||||
Rigidbody:
|
Rigidbody:
|
||||||
m_ObjectHideFlags: 0
|
m_ObjectHideFlags: 0
|
||||||
|
@ -6,11 +6,22 @@ public class MovingSphere : MonoBehaviour
|
|||||||
private float maxSpeed = 10f;
|
private float maxSpeed = 10f;
|
||||||
|
|
||||||
[SerializeField, Range(0f, 100f)]
|
[SerializeField, Range(0f, 100f)]
|
||||||
private float maxAcceleration = 10f;
|
private float maxAcceleration = 10f, maxAirAcceleration = 1f;
|
||||||
|
|
||||||
|
[SerializeField, Range(0f, 10f)]
|
||||||
|
float jumpHeight = 2f;
|
||||||
|
|
||||||
|
[SerializeField, Range(0, 5)]
|
||||||
|
int maxAirJumps = 0;
|
||||||
|
|
||||||
private Rigidbody body;
|
private Rigidbody body;
|
||||||
|
|
||||||
private Vector3 desiredVelocity;
|
private Vector3 desiredVelocity = new Vector3(0f, 0f, 0f);
|
||||||
|
private bool desiredJump = false;
|
||||||
|
|
||||||
|
private bool onGround;
|
||||||
|
private int jumpPhase;
|
||||||
|
private Vector3 velocity;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
@ -24,14 +35,70 @@ public class MovingSphere : MonoBehaviour
|
|||||||
playerInput.y = Input.GetAxis("Vertical");
|
playerInput.y = Input.GetAxis("Vertical");
|
||||||
playerInput = Vector2.ClampMagnitude(playerInput, 1f);
|
playerInput = Vector2.ClampMagnitude(playerInput, 1f);
|
||||||
desiredVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
|
desiredVelocity = new Vector3(playerInput.x, 0f, playerInput.y) * maxSpeed;
|
||||||
|
|
||||||
|
desiredJump |= Input.GetButtonDown("Jump");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void FixedUpdate()
|
private void FixedUpdate()
|
||||||
{
|
{
|
||||||
var velocity = body.velocity;
|
UpdateState();
|
||||||
float maxSpeedChange = maxAcceleration * Time.deltaTime;
|
|
||||||
|
float acceleration = onGround ? maxAcceleration : maxAirAcceleration;
|
||||||
|
float maxSpeedChange = acceleration * Time.deltaTime;
|
||||||
velocity.x = Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
|
velocity.x = Mathf.MoveTowards(velocity.x, desiredVelocity.x, maxSpeedChange);
|
||||||
velocity.z = Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
|
velocity.z = Mathf.MoveTowards(velocity.z, desiredVelocity.z, maxSpeedChange);
|
||||||
|
|
||||||
|
if (desiredJump)
|
||||||
|
{
|
||||||
|
desiredJump = false;
|
||||||
|
Jump();
|
||||||
|
}
|
||||||
|
|
||||||
body.velocity = velocity;
|
body.velocity = velocity;
|
||||||
|
|
||||||
|
onGround = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void UpdateState()
|
||||||
|
{
|
||||||
|
velocity = body.velocity;
|
||||||
|
if (onGround)
|
||||||
|
{
|
||||||
|
jumpPhase = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnCollisionEnter(Collision collision)
|
||||||
|
{
|
||||||
|
EvaluateCollision(collision);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnCollisionStay(Collision collision)
|
||||||
|
{
|
||||||
|
EvaluateCollision(collision);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void EvaluateCollision(Collision collision)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < collision.contactCount; i++)
|
||||||
|
{
|
||||||
|
var normal = collision.GetContact(i).normal;
|
||||||
|
onGround |= normal.y >= 0.9f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Jump()
|
||||||
|
{
|
||||||
|
if (onGround || jumpPhase < maxAirJumps)
|
||||||
|
{
|
||||||
|
jumpPhase++;
|
||||||
|
float jumpSpeed = Mathf.Sqrt(-2f * Physics.gravity.y * jumpHeight);
|
||||||
|
if (velocity.y > 0f)
|
||||||
|
{
|
||||||
|
jumpSpeed = Mathf.Max(jumpSpeed - velocity.y, 0f);
|
||||||
|
}
|
||||||
|
velocity.y += jumpSpeed;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user