public class InputAnnouncer : MonoBehaviour {
public static InputAnnouncer Instance;
public delegate void JumpListener();
public event JumpListener OnJump;
public void Awake() {
Instance = this;
}
public void OnDestroy() {
Instance = null;
}
public void FireOnJump() {
if (OnJump != null) {
OnJump();
}
}
}
public class PcInput : MonoBehaviour {
private InputAnnouncer Announcer;
public void Awake() {
Announcer = GetComponent<InputAnnouncer>();
}
public void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
Announcer.FireOnJump();
}
}
}
public class PlayerMovement : MonoBehaviour {
public void OnEnable() {
InputAnnouncer.Instance.OnJump += OnJump;
}
public void OnDisable() {
InputAnnouncer.Instance.OnJump -= OnJump;
}
private void OnJump() {
// Increase the player's upward velocity
}
}