OnCollisionEnter | そのオブジェクトが別のオブジェクトに触れた時に一度だけ発生する。 |
OnCollisionStay | オブジェクトが別のオブジェクトに接触している間、連続して発生し続ける。 |
OnCollisionExit | 接触しているオブジェクトが離れたときに一度だけ発生する。 |
void OnCollisionEnter (Collision collision) { ... } ; void OnCollisionStay (Collision collision) { ... } ; void OnCollisionExit (Collision collision) { ... } ;
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; } } }
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.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; } } }