销毁: Destroy(gameObject,time);
//Clamp是约束的方法,第一个参数是要给定的值,第二个参数是要限制的最小值,第三个参数要限制的最大值.
float m_x = Mathf.Clamp (transform.position.x, -3f, 3f);
//将做好约束的数值复制给想要约束的单位
transform.position = new Vector3 (m_x, transform.position.y, transform.position.z);
//效果:无论怎么改变Cube的X坐标永不会超过[-3,3]
如何创一堵墙:
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
Instantiate (m_CubePrefab, new Vector3 (i, j, 0), Quaternion.identity);
}
}
使用Lerp 和Distan 配合实现 摄像头追随
float dis = Vector3.Distance (transform.position, m_Leader.transform.position);
if (dis > 15) {
transform.position = Vector3.Lerp (transform.position, m_Leader.transform.position, 0.3f);
}
//子弹CD
private float m_ShootTime = 1f;
//计时器
private float m_timer = 0;
//在Update里让计时器递增
m_timer += Time.deltaTime;
if (Input.GetKey (KeyCode.Space) && m_timer >= m_ShootTime) {
Instantiate (m_bullet, m_weizhi.position, Quaternion.identity);
m_timer = 0; //执行后计时器清零
}