Created
June 16, 2016 04:30
-
-
Save s2kw/bc70527a520d32992275c291d0fcf273 to your computer and use it in GitHub Desktop.
毎日出題の4日目第三問 description: http://jigax.jp/unityunityとcを学ぶ問題004/ movie:https://vimeo.com/170791709
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 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