Skip to content

Instantly share code, notes, and snippets.

@ironfroggy
Created November 10, 2015 03:49
Show Gist options
  • Save ironfroggy/63a76a673ce414711a39 to your computer and use it in GitHub Desktop.
Save ironfroggy/63a76a673ce414711a39 to your computer and use it in GitHub Desktop.
public class InputManager : StateMachineBehavior<InputManager.InputState> {
public class InputState : State {
public virtual void OnHits(RaycastHit[] hits) {
}
}
public class StateIdle : InputState {
public override void OnHits(RaycastHit[] hits) {
if (hits.Length > 0) {
States.GetState<StateHover>().Activate(hits[0].collider.gameObject);
}
}
}
public class StateHover : InputState {
GameObject hoverObject;
public void Activate(GameObject obj) {
hoverObject = obj;
base.Activate();
}
public override void OnHits(RaycastHit[] hits) {
bool stillColliding = System.Array.Exists(hits, element => element.collider.gameObject == hoverObject);
if (!stillColliding) {
States.GetState<StateIdle>().Activate();
}
}
}
// Update is called once per frame
void Update () {
RaycastHit[] hits;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
hits = Physics.RaycastAll(ray, 1000.0f);
GetState().OnHits(hits);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment