毎日出題の4日目第三問 description: http://jigax.jp/unityunityとcを学ぶ問題004/ movie:https://vimeo.com/170791709
public class BounceBall : MonoBehaviour { | |
Material mat; | |
enum State | |
{ | |
red = 0,blue = 1,green = 2,free = 3 | |
} | |
State state = State.red; | |
void Start() | |
{ | |
this.mat = GetComponent<Renderer>().material; | |
this.mat.color = Color.red; | |
} | |
void Update() | |
{ | |
if ( this.state == State.free ) | |
{ | |
this.mat.color = new Color( | |
UnityEngine.Random.Range(0f, 1f), | |
UnityEngine.Random.Range(0f, 1f), | |
UnityEngine.Random.Range(0f, 1f) | |
); | |
} | |
} | |
void OnCollisionEnter( Collision col ) | |
{ | |
this.state = this.IncreaseState(); | |
switch ( this.state ) | |
{ | |
case State.red: | |
this.mat.color = Color.red; | |
break; | |
case State.blue: | |
this.mat.color = Color.blue; | |
break; | |
case State.green: | |
this.mat.color = Color.green; | |
break; | |
default: | |
break; | |
} | |
} | |
State IncreaseState() | |
{ | |
if ( System.Enum.GetValues(typeof(State)).Length - 1 <= (int)this.state ) | |
{ | |
return 0; | |
} | |
return this.state + 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment