操纵的方块上面的脚本:
using UnityEngine;
using System.Collections;
public class playerScript : MonoBehaviour {
delegate void CHANGECOLOR(Color color);
event CHANGECOLOR changeMyColor;
event CHANGECOLOR changeMyColor1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
float a = Input.GetAxis ("Horizontal");
float b = Input.GetAxis ("Vertical");
transform.position +=transform.forward* b * 10 * Time.deltaTime;
if(b<0){
b = -b;
}
transform.Rotate (Vector3.up * a *50 *Time.deltaTime);
if (Input.GetKeyDown(KeyCode.Space)) {
//判断事件不为空
if (changeMyColor != null ) {
changeMyColor (Color.yellow);
}
if (changeMyColor1 != null) {
changeMyColor1 (Color.red);
}
}
}
void OnCollisionEnter(Collision other){
//事件绑定
if (other.collider.tag == "cube") {
changeMyColor += other.collider.GetComponent<ChangecolorScript> ().ChangeColor;
}
if (other.collider.tag == "Player") {
changeMyColor1 += other.collider.GetComponent<Changecolor1Script> ().ChangeColor;
}
}
}
被碰撞的其他物体的代码
物体一
using UnityEngine;
using System.Collections;
public class ChangecolorScript : MonoBehaviour {
//player绑定的方法
public void ChangeColor(Color color){
GetComponent<MeshRenderer> ().material.color = color;
}
}
物体二
using UnityEngine;
using System.Collections;
public class Changecolor1Script : MonoBehaviour {
public void ChangeColor(Color color){
GetComponent<MeshRenderer> ().material.color = color;
}
}