刚体:
Rigidbody r;
void Start()
{
r = GetComif (Input.GetMouseButtonDown(0))
{//点击鼠标左键为球体添加刚体组件
Rigidbody r = gameObject.AddComponent<Rigidbody>();
//给球体一个向前的力
r.AddForce(Vector3.forward * 1000);
//获取刚体组件
//Rigidbody r2 = gameObject.GetComponent<Rigidbody>();
//r2.useGravity = false;//去掉重力
}
碰撞检测:
碰撞器
碰撞接触的那一帧被调用
void OnCollisionEnter(Collision c)
{
Debug.Log("enter");
Debug.Log(collision.gameObject.name);
}
碰撞接触的每帧都被调用
void OnCollisionStay(Collision c)
{
Debug.Log("Stay");
}
碰撞结束被调用
void OnCollisionExit(Collision c)
{
Debug.Log("Exit");
}
触发器可以穿透,所以把重力去掉
// 触发接触的那一帧被调用
void OnTriggerEnter(Collider other)
{
Debug.Log("enter");
Debug.Log(other.gameObject.name);
}
//触发接触的每帧都被调用
void OnTriggerStay(Collider other)
{
Debug.Log("Stay");
}
// 触发结束被调用
void OnTriggerExit(Collider other)
{
Debug.Log("Exit");
}
标签:
private GameObject cube, cube2,cube3;
void Start () {
//通过游戏对象名称查找游戏对象
cube = GameObject.Find("cube");
cube2 = GameObject.Find("cube2");
print(cube.name + "\t" + cube2.name);
//cube3 = GameObject.FindWithTag("Cube");
//cube3 = GameObject.FindGameObjectWithTag("Cube");
//print(cube3.name);
//通过标签名查找游戏对象
GameObject[] gos = GameObject.FindGameObjectsWithTag("Cube");
foreach (var item in gos)
{
print(item.name);
}
}