Let's create an event to fire off any time our player jumps:
- Open up PlayerBehavior and add in the following changes:
public class PlayerBehavior : MonoBehaviour
{
// ... No other variable changes needed ...
// 1
public delegate void JumpingEvent();
// 2
public event JumpingEvent playerJump;
void Start()
{
// ... No changes needed ...
}
void Update()
{
_vInput = Input.GetAxis("Vertical") * moveSpeed;
_hInput = Input.GetAxis("Horizontal") * rotateSpeed;
}
void FixedUpdate()
{
if(IsGrounded() && Input.GetKeyDown(KeyCode.Space))
{
_rb.AddForce(Vector3.up * jumpVelocity,
ForceMode.Impulse);
// 3
playerJump();
}
}
// ... No changes needed in IsGrounded or OnCollisionEnter
}
Let's break down the code:
- Declares a new delegate type that...