阿里巴巴網(wǎng)站分類板塊做全屏全網(wǎng)營(yíng)銷推廣方案
游戲效果
游戲中:
游戲中止:
一、制作參考
如何制作游戲?【15分鐘】教會(huì)你制作Unity小恐龍游戲!新手15分鐘內(nèi)馬上學(xué)會(huì)!_ unity教學(xué) _ 制作游戲 _ 游戲開發(fā)_嗶哩嗶哩_bilibili
二、圖片資源
https://download.csdn.net/download/benben044/89522911?spm=1001.2014.3001.5501
?三、創(chuàng)建場(chǎng)景
1、將資源都拖到Assets目錄下
2、調(diào)整編輯布局
將Scene、Hierarchy、Game這3個(gè)模塊分開,方便可以同時(shí)觀察到三個(gè)模塊的信息。
同時(shí)將Main Camera的color設(shè)置為白色,方便看到ground的顏色。
3、放dinosaur到scene
將assets中的run-2放到Scene中,就可以在Game中看到dinosaur的圖案,同時(shí)將恐龍rename為Dinosaur。
?4、添加cloud到scene
同時(shí)給cloud進(jìn)行重命名。
5、給dinosaur和ground添加組件
(1)給dinosaur添加Rigidbody 2d組件,使其具有物理屬性,設(shè)置Gravity Scale為2,重力大一點(diǎn)下降速度會(huì)更快一點(diǎn)。
(2)再給dinosaur添加Box Collider 2d組件,使其具有碰撞屬性
(3)接著給ground添加Box Collider 2d組件,使其具有碰撞屬性
通過以上操作,dinosaur因?yàn)橛形锢韺傩运詴?huì)自然掉落,但是因?yàn)閐inosaur和ground都有碰撞屬性,所以dinosaur掉到地面后就停止掉落了。
在測(cè)試中如果發(fā)現(xiàn)Dinosaur掉到地面后腳沒有落地,可以編輯Box Collider 2D中的Edit Collider,調(diào)整碰撞的范圍。
四、編輯C#腳本
1、創(chuàng)建恐龍腳本
public class Dinosaur : MonoBehaviour
{Rigidbody2D rb;bool isJumping;public float jump; // 當(dāng)為public時(shí)即可在Inspector面板看到屬性信息// Start is called before the first frame updatevoid Start(){rb = GetComponent<Rigidbody2D>();isJumping = false;}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Space) && isJumping == false) { rb.velocity = new Vector2(0, jump); isJumping = true;}}private void OnCollisionEnter2D(Collision2D collision){// 只有碰到底才能跳,否則在天空中就飛起來了isJumping = false; }
}
在Dinosaur的Dinosaur組件中設(shè)置jump為8。
2、創(chuàng)建恐龍動(dòng)畫
首先,在Assets下創(chuàng)建Animations的目錄。
在Window->Animation下創(chuàng)建動(dòng)畫組件,然后點(diǎn)擊Scene下的Dinosaur,最后點(diǎn)擊Create,在Assets下的Animations下創(chuàng)建DinosaurAnimation.anim文件,如下圖所示:
然后將dinosaur_assets下的run-1和run-2拖入animation編輯器,第1個(gè)點(diǎn)是run-1,第2個(gè)點(diǎn)是run-2,第3個(gè)點(diǎn)是run-1,循環(huán)的動(dòng)作。
3、創(chuàng)建Movement腳本
在Assets下創(chuàng)建Movement的腳本,該腳本的功能是:當(dāng)角色在run時(shí),天上的云、地面都在向左移動(dòng)。即,角色不動(dòng),參照物動(dòng),從而造成角色向右移動(dòng)的假象。
public class Movement : MonoBehaviour
{public float movementSpeed;public float startPosition;public float endPosition;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.position = new Vector2(transform.position.x - movementSpeed * Time.deltaTime, transform.position.y);// 到dinosaur快要越界掉下去時(shí),從開始處重新開始runif (transform.position.x <= endPosition){transform.position = new Vector2(startPosition, transform.position.y);}}
}
給ground,所有的cloud都加載該腳本,并且設(shè)置3個(gè)變量分別為5,5,-15。
4、加入仙人掌
將仙人掌放到Scene中,并且添加Box Collider 2D的腳本,這樣恐龍就不能穿透它了。同時(shí)加入Movement的腳本,參數(shù)設(shè)置也是5,5,-15。
5、創(chuàng)建GameManager
在Scene中Create Empty,名為GameManager。
然后在Assets下創(chuàng)建GameManager的腳本,并把該腳本賦予GameManager的物體上。
腳本內(nèi)容如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class GameManager : MonoBehaviour
{public GameObject cactus; // 仙人掌public GameObject cactusSpawnPostition; //仙人掌出現(xiàn)的位置public float spawnTime; // 仙人掌出現(xiàn)的時(shí)間float timer; // 計(jì)時(shí)器// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){timer += Time.deltaTime;if (timer >= spawnTime){Instantiate(cactus, cactusSpawnPostition.transform);timer = 0;}}
}
意思是:當(dāng)超過spawnTime時(shí),就讓cactus在cactusSpawnPosition出現(xiàn)。?
6、創(chuàng)建cactusSpawnPosition
這個(gè)就是上個(gè)腳本中仙人掌每次自動(dòng)生成的位置。
修改該對(duì)象的tag為紅色菱形,這樣在scene中更容易被辨認(rèn)出來。
然后在scene中確定位置,如下圖所示:
接著拖動(dòng)hierachy中的cactus到Assets成為prefab預(yù)制體,修改器transform中的的Position全是0。
然后,設(shè)置GameManager的參數(shù)如下:
7、給仙人掌添加tag
點(diǎn)擊Asset中的cactus_single,然后在其Tag旁邊的Untagged點(diǎn)擊一下,選擇Add Tag如下圖:
添加Cacuts的tag如下:
在Tag欄位選擇剛剛創(chuàng)建的Cactus,如下:
8、刪除不用的仙人掌
如果仙人掌已經(jīng)在movement中endPosition的左邊,就可以destory它。
所以,如果越界后,是仙人掌就destroy它,否則類似ground、cloud就循環(huán)從頭開始。
所以優(yōu)化Movement腳本如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Movement : MonoBehaviour
{public float movementSpeed;public float startPosition;public float endPosition;// Start is called before the first frame updatevoid Start(){}// Update is called once per framevoid Update(){transform.position = new Vector2(transform.position.x - movementSpeed * Time.deltaTime, transform.position.y);// 到dinosaur快要越界掉下去時(shí),從開始處重新開始runif (transform.position.x <= endPosition){if(gameObject.tag == "Cactus"){Destroy(gameObject);}else{transform.position = new Vector2(startPosition, transform.position.y);}}}
}
9、創(chuàng)建GameOver和Restart
(1)GameOver
在Hierarchy中選擇UI -> Legacy -> Text如下:
編寫文字:GAME OVER
并且選擇好FFT格式的字體。
(2)Restart
在Cavas下再創(chuàng)建Button,位于UI -> Legacy -> Button,重命名為RestartButton。
然后將Button的圖標(biāo)替換為已有的素材,在調(diào)整下size。
?(3)創(chuàng)建Panel
將上面創(chuàng)建的2個(gè)UI組件都裝到該P(yáng)anel下,同時(shí)調(diào)整Panel->Color->A值為0。
這樣,同時(shí)控制Panel的Active狀態(tài),就可以控制Panel的出現(xiàn)和消失。
只有當(dāng)觸發(fā)GameOver時(shí)該P(yáng)anel才會(huì)出現(xiàn),否則該P(yáng)anel的Active為false。
10、 保存場(chǎng)景
在File->Save As中,在Assets->Scenes下保存場(chǎng)景為lv1,level1的意思。
11、在GameManager中加入GameOver和Restart
- 創(chuàng)建GameOverScene變量,對(duì)應(yīng)GameOver的Panel
- Start()中加入控制速度的Time.scaleTime=1
- 在多個(gè)函數(shù)中設(shè)置GameOver的Panel的Active為false
- 在GameOver()中,設(shè)置Time.scaleTime=0,同時(shí)出現(xiàn)GameOver的Panel
- 在Restart()中,重新加載lv1的scene,同時(shí)GameOver的Panel的Active為false
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;public class GameManager : MonoBehaviour
{public GameObject cactus; // 仙人掌public GameObject cactusSpawnPostition; //仙人掌出現(xiàn)的位置public float spawnTime; // 仙人掌出現(xiàn)的時(shí)間float timer; // 計(jì)時(shí)器public GameObject GameOverScene;// Start is called before the first frame updatevoid Start(){Time.timeScale = 1.0f;GameOverScene.SetActive(false);}// Update is called once per framevoid Update(){timer += Time.deltaTime;if (timer >= spawnTime){Instantiate(cactus, cactusSpawnPostition.transform);timer = 0;}}public void GameOver(){Time.timeScale = 0; // 將游戲暫停GameOverScene.SetActive(true);}public void Restart(){SceneManager.LoadScene("lv1");GameOverScene.SetActive(false);}
}
編寫完代碼后,在Inspector中配置GameOverScene的值。
12、在Dinosaur腳本中加入游戲暫停的觸發(fā)條件
主要是在OnCollisionEnter2D中加入觸發(fā)條件。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Dinosaur : MonoBehaviour
{Rigidbody2D rb;bool isJumping;public float jump; // 當(dāng)為public時(shí)即可在Inspector面板看到屬性信息public GameManager gm;// Start is called before the first frame updatevoid Start(){rb = GetComponent<Rigidbody2D>();isJumping = false;}// Update is called once per framevoid Update(){if (Input.GetKeyDown(KeyCode.Space) && isJumping == false) { rb.velocity = new Vector2(0, jump); isJumping = true;}}private void OnCollisionEnter2D(Collision2D collision){// 只有碰到底才能跳,否則在天空中就飛起來了isJumping = false; if(collision.gameObject.tag == "Cactus"){// 撞到仙人掌,游戲暫停gm.GameOver();}}
}
編寫完代碼后,配置GameManager的變量。
至此,該游戲制作完成!
五、總結(jié)
1、檢測(cè)碰撞:OnCollisionEnter2D,這個(gè)是和Update平級(jí)的函數(shù)
2、物體移動(dòng)時(shí),transform.position = new Vector2(x, y),其中x是transform.position.x - Time.deltaTime * speed.
3、時(shí)間是float類型的數(shù)據(jù)
4、實(shí)例化預(yù)設(shè)體用Instantiate函數(shù),參數(shù)1是實(shí)例化的對(duì)象,參數(shù)2是transform信息,可以用某個(gè)物體的transform信息作為參數(shù)2
5、檢測(cè)碰撞的物體時(shí),可以給對(duì)方物體一個(gè)tag,然后通過collision.gameobject.tag == <tag>判斷和該物體是否相撞
6、加載場(chǎng)景使用SceneManager.LoadScene("<場(chǎng)景名稱>")
7、如果有多個(gè)UI需要一起控制,則統(tǒng)一放到Panel中,然后控制該P(yáng)anel即可
8、控制暫停、重啟,可使用時(shí)間變量Time.timeScale
9、腳本被掛載的當(dāng)前物體為transform.gameObject