访问组件 GetComponent-<组件名>().组件属性
查找指定对象 GameObject.Find()
移动对象 TransFrom.Translate()
重力(刚体组件) Rigidbody
1.效果
2.脚本
cs
public int num = 120;
private Rigidbody rb;
private GameObject red;
private GameObject yellow;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.useGravity = true;
red = GameObject.Find("red");
yellow = GameObject.Find("yellow");
}
void Update()
{
// 让红色方块向前移动
red.GetComponent<Transform>().Translate(0, 0, 0.01f);
// 让黄色方块向右移动
yellow.GetComponent<Transform>().Translate(0.01f, 0, 0);
// 让黄色方块转起来
yellow.GetComponent<Transform>().Rotate(10f, 0, 0);
//if (num % 2 == 0)
//{
// Debug.Log(num + "是偶数");
//}
//else
//{
// Debug.Log(num + "是奇数");
//}
}