You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
813 B
C#
35 lines
813 B
C#
using UnityEngine;
|
|
|
|
public class Jump : MonoBehaviour
|
|
{
|
|
Rigidbody rigidbody;
|
|
public float jumpStrength = 2;
|
|
public event System.Action Jumped;
|
|
|
|
[SerializeField, Tooltip("Prevents jumping when the transform is in mid-air.")]
|
|
GroundCheck groundCheck;
|
|
|
|
|
|
void Reset()
|
|
{
|
|
// Try to get groundCheck.
|
|
groundCheck = GetComponentInChildren<GroundCheck>();
|
|
}
|
|
|
|
void Awake()
|
|
{
|
|
// Get rigidbody.
|
|
rigidbody = GetComponent<Rigidbody>();
|
|
}
|
|
|
|
void LateUpdate()
|
|
{
|
|
// Jump when the Jump button is pressed and we are on the ground.
|
|
if (Input.GetButtonDown("Jump") && (!groundCheck || groundCheck.isGrounded))
|
|
{
|
|
rigidbody.AddForce(Vector3.up * 100 * jumpStrength);
|
|
Jumped?.Invoke();
|
|
}
|
|
}
|
|
}
|