unity吃豆人小游戲,迷宮實作
展示效果

這個小游戲主要是做了主角的移動,場景切換,碰撞檢測,
1主角移動
int n = 6;
float H = Input.GetAxis("Horizontal") * Time.deltaTime * n;
float V = Input.GetAxis("Vertical") * Time.deltaTime * n;
this.gameObject.transform.Translate(H, V, 0);//移動函式
2場景切換
a.transform.localPosition = new Vector3(-100F, 10.3F, -0.87F);
b.transform.localPosition = new Vector3(0, 0, -0);
this.gameObject.transform.localPosition = new Vector3(-22.1F, 10.3F, -0.87F);
大家仔細看gif圖片,我level1和level2上面的切換非常流暢,因為我并不是替換scene,對于一些小游戲來說,盡量不要隨便用scene的切換,不然會感覺有卡頓,我這里只是做了位置的主體轉變而已,
3碰撞檢測
//碰撞函式
void OnTriggerEnter(Collider other)
{
//碰到的是球
if (other.name == "ball")
{
Destroy(other.gameObject);
print(ballcount);
ballcount -= 1;
if (ballcount == 0 )
{
if (guanqia == 1)//下一關
{
a.transform.localPosition = new Vector3(-100F, 10.3F, -0.87F);
b.transform.localPosition = new Vector3(0, 0, -0);
this.gameObject.transform.localPosition = new Vector3(-22.1F, 10.3F, -0.87F);
ballcount = 3;
//關卡+1
guanqia = guanqia + 1;
}
else//贏了
{
print("贏了");
}
}
}
//碰到的是墻
else
{
//回傳原來的位置
this.gameObject.transform.localPosition = new Vector3(-22.1F, 10.3F, -0.87F);
}
}
這里要是碰到小球就算分數,進入下一關,要是碰到的是墻,就要回到原來的位置,
有需要原始碼的小伙伴可以聯系我,也歡迎其他小伙伴留言交流學習

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/266737.html
標籤:其他
