#Advanced Jump
4 messages · Page 1 of 1 (latest)
using UnityEngine;
public class PlayerJump : MonoBehaviour
{
[Header("Jump Ray Cast Settings/Jump Settings")]
public Vector2 boxSize;
public float boxDistance;
public LayerMask layerMask;
bool canJump;
public float maxJumpTimer;
float jumpTimer;
float gravity;
public float gravityMultiplier;
public float jumpSpeed;
bool jumping;
[Header("Ground Raycasts")]
public Vector2 groundSize;
public float groundDistance;
there's the variables
public bool IsGrounded()
{
if (Physics2D.BoxCast(transform.position, boxSize, 0, -transform.up, boxDistance, layerMask))
{
return true;
}
else
{
return false;
}
}
public bool TouchingGround()
{
if (Physics2D.BoxCast(transform.position, groundSize, 0, -transform.up, groundDistance, layerMask))
{
return true;
}
else
{
return false;
}
}
public void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position - transform.up * boxDistance, boxSize);
Gizmos.DrawWireCube(transform.position - transform.up * groundDistance, groundSize);
}
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
jumpSpeed *= Time.deltaTime;
}
// Update is called once per frame
void Update()
{
if (IsGrounded())
{
jumpTimer = maxJumpTimer;
}
else
{
gravity -= 0.1f * Time.deltaTime * gravityMultiplier;
}
if (jumpSpeed > 0) canJump = true;
else if (jumpSpeed <= 0) canJump = false;
if (Input.GetKey(KeyCode.Space))
{
jumping = true;
}
else
{
jumping = false;
}
if (canJump)
{
if (jumping)
{
gravity = jumpSpeed;
jumpTimer -= 1 * Time.deltaTime;
}
}
if (TouchingGround())
{
if (!Input.GetKey(KeyCode.Space))
{
gravity = 0;
}
}
}
void LateUpdate()
{
transform.position -= Vector3.down * gravity;
}
}
There's the rest