OnCollisionEnter | そのオブジェクトが別のオブジェクトに触れた時に一度だけ発生する。 |
OnCollisionStay | オブジェクトが別のオブジェクトに接触している間、連続して発生し続ける。 |
OnCollisionExit | 接触しているオブジェクトが離れたときに一度だけ発生する。 |
オブジェクトの衝突判定は、各オブジェクトに組み込まれているコライダーのオブジェクトによって管理されている。
オブジェクトのメッシュなどによってチェックされているわけではない。
リジッドボディが組み込まれている場合は、これによっても判定される。
オブジェクトのメッシュなどによってチェックされているわけではない。
リジッドボディが組み込まれている場合は、これによっても判定される。
void OnCollisionEnter (Collision collision) { ... } ; void OnCollisionStay (Collision collision) { ... } ; void OnCollisionExit (Collision collision) { ... } ;
衝突した接点の情報を保管。この値は、「ContactPoint」というクラスの配列になっている。
接点が複数ある場合もあるので、1つ1つの接点がContactPointインスタンスとしてすべてまとめられている。
接点が複数ある場合もあるので、1つ1つの接点がContactPointインスタンスとしてすべてまとめられている。
OnCollisionEnter | 他のColliderやRigidbodyオブジェクトに触れた時に呼び出される |
OnCollisionExit | 他のColliderやRigidbodyオブジェクトに離れた時に呼び出される |
OnCollisionStay | 他のColliderやRigidbodyオブジェクトに触れ続けている時に呼び出される |
private void OnCollisionStay(Collision c){ if (c.gameObject.name == "Ball"){ Destroy (c.gameObject); } }
private void OnCollisionStay(Collision c){ if (c.gameObject.name == "Ball"){ Destroy (gameObject); } }
private void OnCollisionStay(Collision c){ if (c.gameObject.CompareTag("Ball")){ Destroy (gameObject); } }
public class myscript : MonoBehaviour { void Start () { GameObject.Find("GUI Text").GetComponent<GUIText>().text ="Collision Event sample"; } void Update () { GameObject cube = GameObject.Find ("Cube"); cube.transform.Rotate (1f, -1f, -1f); if (Input.GetKey (KeyCode.LeftArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (-1f, 0, 1f)); } if (Input.GetKey (KeyCode.RightArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (1f, 0, -1f)); } if (Input.GetKey (KeyCode.UpArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (1f, 0, 1f)); } if (Input.GetKey (KeyCode.DownArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (-1f, 0, -1f)); } } void OnCollisionEnter(Collision collision){ // 衝突した相手がPlaneでなければ処理をする if (collision.gameObject.name != "plane") { // 衝突した対象(collisionオブジェクト)の色を変更している。 collision.gameObject.GetComponent<Renderer>().material.color = Color.yellow; } } }
public class myscript : MonoBehaviour { int counter = 0; GameObject obj = null; void Start () { GameObject.Find("GUI Text").GetComponent<GUIText>().text ="Collision Event sample"; } void Update () { GameObject cube = GameObject.Find ("Cube"); // objの値をチェックし、何かのオブジェクトが保管されているなら、counterを増やして、 // 100より大きくなっていたら元の状態に戻している。 if(obj != null){ if (counter++ > 100){ obj.SetActive (true); obj = null; } } try { cube.transform.Rotate (1f, -1f, -1f); } catch (System.NullReferenceException e){} if (Input.GetKey (KeyCode.LeftArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (-1f, 0, 1f)); } if (Input.GetKey (KeyCode.RightArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (1f, 0, -1f)); } if (Input.GetKey (KeyCode.UpArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (1f, 0, 1f)); } if (Input.GetKey (KeyCode.DownArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (-1f, 0, -1f)); } } void OnCollisionEnter(Collision collision){ if (collision.gameObject.tag == "Player"){ if (obj != null) { obj.SetActive (true); } collision.gameObject.SetActive (false); obj = collision.gameObject; counter = 0; } } }
ゲームオブジェクトを透明に表示するには「シェーダー」を変更し、表示する色などを調整する。
シェーダーというのは、オブジェクトを描画する方法などを定義したもの。
ゲームオブジェクトを作成すると、色やテクスチャーを設定すればそのままシーンに表示されるが、これはUnityに用意されているシェーダーがデフォルトでされており、「このオブジェクトをどういうやり方で描くか」が決まっていたから。
色の塗り方、光があたった時の反射や透明な状態の表示など、細かな描き方がシェーダーによって決められている。
デフォルトで設定されているのは、「Diffuse」という、不透明でもっとも一般的な描き方のシェーダー。
これを「半透明な、一般的な描き方」のシェーダーに変える。
シェーダーというのは、オブジェクトを描画する方法などを定義したもの。
ゲームオブジェクトを作成すると、色やテクスチャーを設定すればそのままシーンに表示されるが、これはUnityに用意されているシェーダーがデフォルトでされており、「このオブジェクトをどういうやり方で描くか」が決まっていたから。
色の塗り方、光があたった時の反射や透明な状態の表示など、細かな描き方がシェーダーによって決められている。
デフォルトで設定されているのは、「Diffuse」という、不透明でもっとも一般的な描き方のシェーダー。
これを「半透明な、一般的な描き方」のシェーダーに変える。
あらかじめ接触される側のシェーダーを変更しておく必要がある。
今回はインスペクターで「GUI/Text Shader」にしておいた。(それがたまたま動いたので)
今回はインスペクターで「GUI/Text Shader」にしておいた。(それがたまたま動いたので)
public class myscript : MonoBehaviour { void OnCollisionEnter(Collision collision){ if (collision.gameObject.name == "Cube"){ collision.gameObject.GetComponent<Renderer>().material.color = new Color (1f, 0, 0, 0.1f); } } void OnCollisionExit (Collision collision){ if (collision.gameObject.name == "Cube"){ collision.gameObject.GetComponent<Renderer>().material.color = new Color(1f, 0, 0, 1f); } } }
オブジェクトで設定されているシェーダーは、Materialの「shader」というプロパティとして管理されている。
これにはShaderクラスのインスタンスが保管されている。
使いたいシェーダーのShaderインスタンスを用意し、それをMaterialのshaderに設定してやれば、スクリプトでシェーダーを操作することができる。
これにはShaderクラスのインスタンスが保管されている。
使いたいシェーダーのShaderインスタンスを用意し、それをMaterialのshaderに設定してやれば、スクリプトでシェーダーを操作することができる。
Shaderインスタンスは、Shaderクラスの「Find」とぴうメソッドを使って取り出すことができる。
引数にShader名を指定すると、そのShaderインスタンスを返してくる。
引数にShader名を指定すると、そのShaderインスタンスを返してくる。
<Material>.shader = Shader.Find (シェーダー名); (例) obj.GetComponent<Renderer>().material.shader = Shader.Find("GUI/Text Shader");
※エラー吐くためダメ
public class myscript : MonoBehaviour { void Start () { GameObject.Find("GUI Text").GetComponent<GUIText>().text ="Collision Event sample"; GameObject[] objs = GameObject.FindGameObjectWithTag ("Player"); foreach (GameObject obj in objs) { Color c = obj.GetComponent<Renderer> ().material.color; c.a = 1f; obj.GetComponent<Renderer> ().material.color = c; obj.GetComponent<Renderer> ().material.shader = Shader.Find ("GUI/Text Shader"); } } void Update () { GameObject cube = GameObject.Find ("Cube"); try { cube.transform.Rotate(1f, -1f, -1f); } catch (System.Exception e){ } if (Input.GetKey (KeyCode.LeftArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (-1f, 0, 1f)); } if (Input.GetKey (KeyCode.RightArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (1f, 0, -1f)); } if (Input.GetKey (KeyCode.UpArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (1f, 0, 1f)); } if (Input.GetKey (KeyCode.DownArrow)) { GetComponent<Rigidbody>().AddForce (new Vector3 (-1f, 0, -1f)); } } void OnCollisionEnter(Collision collision){ if (collision.gameObject.tag == "Player"){ Color c = collision.gameObject.GetComponent<Renderer>().material.color; c.a = 0.25f; collision.gameObject.GetComponent<Renderer>().material.color = c; } } void OnCollisionStay(Collision collision){ } void OnCollisionExit (Collision collision){ if (collision.gameObject.tag == "Player") { Color c = collision.gameObject.GetComponent<Renderer> ().material.color; c.a = 1f; collision.gameObject.GetComponent<Renderer> ().material.color = c; } } }
コメントをかく