小游戏FlyBird:
1.水管无限循环;
2.小鸟原地上下跳跃; Rigidbody:AddForce();
3.小鸟碰到水管,掉落到地上,水管停止运动,游戏结束; 碰撞检测OnCollision;
4.小鸟穿过水管加一分;触发检测OnTrigger;
游戏效果:
Player脚本:
public class FlyDemo : MonoBehaviour
{
public Text ts;//显示分数UI
int score = 0;//分数
Rigidbody rig;
void Start()
{
rig = this.GetComponent<Rigidbody>();
//游戏开始给bird施加一个向上的力
rig.AddForce(Vector3.up * 350);
//指定方向一个速度
//body.velocity = Vector3.up * 5;
ts.text = "分数:" + score;
}
void Update()
{
if (Input.GetButtonDown("Jump"))
{
rig.AddForce(Vector3.up * 300);
}
}
//如果碰撞到就结束游戏
private void OnCollisionEnter(Collision collision)
{
//水管停止运动
GameObject go = GameObject.Find("Plane");
go.GetComponent<PipeMove>().enabled = false;
rig.isKinematic = true;
Debug.Log("游戏结束");
}
private void OnTriggerExit(Collider other)
{
score++;
Debug.Log("分数:" + score);
ts.text = "分数:" + score;
}
}
场景脚本(水管移动):
public class PipeMove : MonoBehaviour
{
public Transform[] pipes;
void Start()
{
}
void Update()
{
//水管无限循环并移动
for (int i=0;i<pipes.Length;i++) {
pipes[i].Translate(-0.7f * Time.deltaTime, 0, 0);
if (pipes[i].position.x<=-7) {
pipes[i].position = new Vector3(9,pipes[i].position.y,pipes[i].position.z);
}
}
}
}
End...