Created
November 10, 2015 03:49
-
-
Save ironfroggy/63a76a673ce414711a39 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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