Created
September 20, 2018 19:43
-
-
Save davepape/ac1c56bb313a119e724236fcd4c4395a to your computer and use it in GitHub Desktop.
This file contains 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
// Example of a simple state machine. | |
// The script has 3 states for different actions it might be performing. It transitions between the states based on user input. | |
// State "start" does nothing; state "slowcube" makes the object named "cube" slow down; state "spiralsphere" makes the sphere object spiral outward. | |
// It can transition from "start" to "slowcube" OR "spiralsphere" depending on what button is hit. | |
// After that, it can transition from "slowcube" to "spiralsphere" or from "spiralsphere" to "slowcube" when fire1 is hit. | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class statemachine : MonoBehaviour { | |
public enum State { start, slowcube, spiralsphere }; | |
public State currentState; | |
// Use this for initialization | |
void Start () { | |
currentState = State.start; | |
} | |
// Update is called once per frame | |
void Update () { | |
UpdateState(); | |
if (currentState == State.start) | |
{} | |
else if (currentState == State.slowcube) | |
SlowTheCube(); | |
else if (currentState == State.spiralsphere) | |
SpiralTheSphere(); | |
} | |
void UpdateState() | |
{ | |
if (currentState == State.start) | |
{ | |
if (Input.GetButtonUp("Fire1")) | |
currentState = State.slowcube; | |
else if (Input.GetButtonUp("Fire2")) | |
currentState = State.spiralsphere; | |
} | |
else if (currentState == State.slowcube) | |
{ | |
if (Input.GetButtonUp("Fire1")) | |
currentState = State.spiralsphere; | |
} | |
else if (currentState == State.spiralsphere) | |
{ | |
if (Input.GetButtonUp("Fire1")) | |
currentState = State.slowcube; | |
} | |
} | |
void SlowTheCube() | |
{ | |
GameObject thecube = GameObject.Find("Cube"); | |
circle1 circlescript = thecube.GetComponent<circle1>(); | |
circlescript.speed -= Time.deltaTime * 5; | |
} | |
void SpiralTheSphere() | |
{ | |
GameObject.Find("Sphere").GetComponent<circle1>().radius += Time.deltaTime * 2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment