日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

2D横版游戏Demo试做流程(Unity3D+Visual Studio C#)

發(fā)布時間:2024/3/26 C# 69 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2D横版游戏Demo试做流程(Unity3D+Visual Studio C#) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

2D橫版游戲Demo試做流程(Unity3D+Visual Studio C#)

  • 目錄
    • UI界面設置
    • 游戲規(guī)則制定
    • Player角色(玩家可控制角色相關)(創(chuàng)建playerC#文件)
    • 怪物類(各種怪物相關)(創(chuàng)建MonsterC#文件)
    • 獎勵物體類(金幣,無敵,攻擊等物品)
    • 界面跳轉
    • 發(fā)布

目錄

UI界面設置

通過各種途徑獲得相關UI元素,在Unity中左側欄中利用2D Project中元素完成場景。設置好各個物體所需的Box Collider 2d (碰撞)和 rigidBody 2d(物理屬性)。設置好不同物體的類型(tag)(例如角色、金幣、怪物、子彈等)后續(xù)碰撞檢測需要使用。
主場景

進入游戲界面

失敗和成功界面


其中還包括設置游戲內(nèi)怪物和角色動畫。角色為UNITY社區(qū)中下載的包,已包含動畫。怪物使用關鍵幀動畫,創(chuàng)建動畫后,在ctri+6呼出的時間軸中添加關鍵幀。

游戲規(guī)則制定

在項目下新建Scriipt(代碼)文件夾,創(chuàng)建GameManager的C#文件。在該文件中制定游戲大體規(guī)則。

使用單例模式(餓漢子單例模式)創(chuàng)建各種變量。

public class GameManage : MonoBehaviour {//單例模式(餓漢子單例模式)public static GameManage Index; //靜態(tài)變量不能被實例化GameManage(){Index = this;}//工廠模式(內(nèi)部所有組成成員的變量全部都是靜態(tài)變量)[Header("金幣值")]public int gold = 0;[Header("當前關卡")]public int Level = 1;/// <summary>/// 所有的音頻管理/// </summary>public AudioSource[] allAudio;/// <summary>/// 地面/// </summary>public GameObject Floor;/// <summary>/// 金幣顯示/// </summary>public Text Gold;/// <summary>/// 剩余時間顯示/// </summary>public Text TimeShow;float times = 300;/// <summary>/// 主角速度/// </summary>public float PlayerSpeed = 0.03f;/// <summary>/// 主角/// </summary>public GameObject KyaRa;/// <summary>/// 主角狀態(tài) 0為無防護/// </summary>public int PlayerState = 0;/// <summary>/// 顯示無敵圖標/// </summary>public GameObject PlayerIsSuper;/// <summary>/// 顯示主角武器/// </summary>public GameObject PlayerIsAttact;// Start is called before the first frame updatevoid Start(){PlayerIsSuper.SetActive(false);PlayerIsSuper.SetActive(false);}// Update is called once per framevoid Update(){times -= Time.deltaTime;TimeShow.text = times.ToString();} }

Player角色(玩家可控制角色相關)(創(chuàng)建playerC#文件)

總體分為幾種功能
1、角色的各種狀態(tài)(創(chuàng)建各種變量標識主角狀態(tài))

//動畫控制public Animator anime;//物理屬性public Rigidbody2D rigi;//檢測是否在地面上(false為不在地面上)bool IsOnFloor = false;//當前人物位置float PlayerPrint;//是否人物移動bool isPlayerRun = true;/// <summary>/// 武器/// </summary>public GameObject Attact;

2、角色控制相關(通過鍵盤按鍵WASD移動,J攻擊操控角色)移動主要使用transform方法改變主角位置。在主角行走至屏幕中間時,改為地圖進行相對向后運動,主角位置不變。

void run(){//點擊D鍵向右移動if (Input.GetKey(KeyCode.D)){if (isPlayerRun){//控制跑動速度和位置transform.Translate(GameManage.Index.PlayerSpeed* - 1, 0, 0);}else{GameManage.Index.Floor.transform.Translate(GameManage.Index.PlayerSpeed * -1, 0, 0);}//控制人物面朝方向為向右transform.eulerAngles = new Vector3(0, 180, 0);if (IsOnFloor ){//播放跑步動畫anime.Play("run");}}//松開前進鍵播放站立動畫if (Input.GetKeyUp(KeyCode.D)){anime.Play("idle");}//點擊A鍵向左移動if (Input.GetKey(KeyCode.A)){transform.Translate(GameManage.Index.PlayerSpeed * -1, 0, 0);//控制人物面朝方向為向左transform.eulerAngles = new Vector3(0, 0, 0);if (IsOnFloor){//播放跑步動畫anime.Play("run");}}//松開前進鍵播放站立動畫if (Input.GetKeyUp(KeyCode.A)){anime.Play("idle");}//點擊W鍵控制跳躍if (IsOnFloor){if (Input.GetKeyDown(KeyCode.W)){//給定一個向上的力rigi.AddForce(new Vector2(0, 400));anime.Play("jump");}}//松開上鍵播放站立動畫if (Input.GetKeyUp(KeyCode.W)){anime.Play("idle");}}

此外還要設定主角在跳躍過程中不能再進行二次跳躍。在開頭變量中設置有變量角色是否在地面上(利用碰撞信息進行判定)在角色控制中使用相關控制語句,在下方的碰撞檢測中更改相關變量。

3、角色攻擊相關
在角色吃到特定物品后,將GamaManager中的主角狀態(tài)變量更改,在判斷主角狀態(tài)為可攻擊狀態(tài)時,可以進行攻擊。
角色攻擊方法(創(chuàng)建一個子彈,并利用rigidBody 2d給其一個力使其移動,子彈的消失和碰撞到怪物殺死怪物消失,后續(xù)進行解釋)創(chuàng)建子彈使用Instantiate方法,在主角當前位置創(chuàng)建。

void Attacts(){if (Input.GetKeyDown(KeyCode.J)){if (GameManage.Index.PlayerState == 1){GameObject ss = Instantiate(Attact, this.transform.position , this.transform.rotation );//在player下創(chuàng)建子彈ss.GetComponent<Rigidbody2D>().AddForce(this.transform.right*600*-1);}}}

4、主角死亡的判定
利用碰撞檢測和得到的當前主角狀態(tài)(若為無敵狀態(tài)則不死亡)進行條件判斷,判斷角色是否死亡,利用switch判斷。

private void OnCollisionEnter2D(Collision2D collision){//Debug.Log(collision.collider.tag);if (collision.collider.tag == "Monster"){//主角死亡switch (GameManage.Index.PlayerState){case 0:anime.Play("die");Die();break;case 1:anime.Play("die");Die();break;case 2:Debug.Log("您現(xiàn)在是無敵狀態(tài)");break;}}IsOnFloor = true;//anime.Play("idle");}

5、角色死亡方法(角色死亡為觸碰到怪物)
主體死亡方法為停止播放當前動畫,并播放死亡動畫和音頻,并利用延遲函數(shù)invoke調(diào)用兩個獨立的死亡方法。

public void Die(){GameManage.Index.allAudio[0].Stop();GameManage.Index.allAudio[4].Play();anime.Play("die");Invoke("Die1", 0.5f);Invoke("Die2", 4f);}

主角死亡后向下掉落出地圖(通過改變BoxCollider 2D 的isTrigger屬性為true,讓主角下落)

void Die1(){this.gameObject.GetComponent<BoxCollider2D>().isTrigger = true;}

主角死亡后界面需要跳轉

void Die2(){SceneManager.LoadScene("fail");}

怪物類(各種怪物相關)(創(chuàng)建MonsterC#文件)

1、各種變量(包括記錄怪物種類,怪物生命值等的各種變量)

public int MonsterState = 0;//0是普通怪物 1是火圈 2是bosspublic Animator anime;public GameObject Fees;public float times = 2;public int BossLive = 10;

2、設置不同怪物的移動方式和出場方法(普通怪物在和角色相距10f時開始向主角移動,boss在和角色相距10f是開始發(fā)射小型怪物)怪物發(fā)射小型怪物原理和上方主角發(fā)射攻擊原理相同。

if (MonsterState == 0){if (Vector3.Distance(GameManage.Index.KyaRa.transform.position, this.transform.position) <= 10f){this.transform.Translate(-0.03f, 0, 0);}}else if (MonsterState == 2){if (Vector3.Distance(GameManage.Index.KyaRa.transform.position, this.transform.position) <= 10f){times -= Time.deltaTime;if (times < 0){GameObject ss =Instantiate(Fees, this.transform.position, this.transform.rotation);//在player下創(chuàng)建子彈ss.GetComponent<Rigidbody2D>().AddForce(this.transform.right * 100 );times = 3;}}}

3、怪物死亡判定。(根據(jù)怪物狀態(tài),不同怪物情況不同,利用碰撞信息判定,判定觸碰到無敵狀態(tài)下的player或子彈都會死亡,boss設定攻擊次數(shù)。)利用tag輔助判斷到底觸碰到了什么物體。

void OnCollisionEnter2D(Collision2D collision){if (collision.collider.tag == "Player"){if (GameManage.Index.PlayerState == 2){Debug.Log("自己死亡");this.gameObject.GetComponent<BoxCollider2D>().isTrigger = true;GameManage.Index.allAudio[3].Play();}}else if (collision.collider.tag == "Attacts" && MonsterState == 0){Debug.Log("自己死亡");Destroy(this.gameObject);GameManage.Index.allAudio[3].Play();GameManage.Index.gold += 10;GameManage.Index.Gold.text = GameManage.Index.gold.ToString();}else if (collision.collider.tag == "Attacts" && MonsterState == 2){BossLive = BossLive - 1;if (BossLive == 0){Debug.Log("自己死亡");Destroy(this.gameObject);GameManage.Index.allAudio[3].Play();GameManage.Index.gold += 100;GameManage.Index.Gold.text = GameManage.Index.gold.ToString();}}else if (collision.collider.tag == "Player" && MonsterState == 1 && GameManage.Index.PlayerState <= 2){anime.Play("die");collision.gameObject.GetComponent<Players>().Die();}} }

獎勵物體類(金幣,無敵,攻擊等物品)

1、設置變量(包括種類標識信息等)

public int state = 0;//0代表金幣1代表武器2代表無敵3代表子彈4代表火圈5代表傳送門int OldState = 0;

2、根據(jù)情況設置不同的碰撞出發(fā)(OnTriggerEnter2D、OnCollisionEnter2D等,并根據(jù)tag輔助判斷碰撞物為角色)

private void Start(){if (state == 3){Invoke("Des", 1f);}}void Des(){Destroy(this.gameObject);}private void Update(){if (state == 4){transform.Rotate(0, 0, 0.3f);}}//碰撞檢測中的觸發(fā)器,進入觸發(fā)器區(qū)域private void OnTriggerEnter2D(Collider2D collision){if (collision.tag == "Player" && state == 0){GameManage.Index.gold += 1;GameManage.Index.Gold.text = GameManage.Index.gold.ToString();GameManage.Index.allAudio[1].Play();Destroy(this.gameObject);}else if (collision.tag == "Player" && state == 2){GameManage.Index.allAudio[5].Play();OldState = GameManage.Index.PlayerState;GameManage.Index.PlayerState = 2;GameManage.Index.PlayerIsSuper.SetActive(true);Invoke("SuperPlayerOver", 5f);Destroy(this.gameObject.GetComponent<SpriteRenderer>());//在沒有刪除物體的情況下,不讓其顯示Destroy(this.gameObject.GetComponent<BoxCollider2D>());}else if (collision.tag == "Player" && state == 1){GameManage.Index.allAudio[2].Play();GameManage.Index.PlayerIsAttact.SetActive(true);GameManage.Index.PlayerState = 1;Destroy(this.gameObject);}else if (collision.tag == "Player" && state == 5){SceneManager.LoadScene("clear");}}private void OnCollisionEnter2D(Collision2D collision){if (collision.collider.tag == "Monster"){Destroy(this.gameObject);}}void SuperPlayerOver(){GameManage.Index.PlayerState = 2;GameManage.Index.PlayerState = OldState;GameManage.Index.allAudio[5].Stop();GameManage.Index.PlayerIsSuper.SetActive(false);//設置是否顯示一個物體Destroy(this.gameObject);}

界面跳轉

按鈕界面的跳轉使用了代碼進行完成

using UnityEngine.SceneManagement;public class Login : MonoBehaviour {//跳轉到制定程序public void GotoScene(string Name){SceneManager.LoadScene(Name);} }

并需要在unity中設置按鈕相關的屬性來實現(xiàn)跳轉。

在代碼頭加入using UnityEngine.SceneManagement;后即可直接利用下方語句直接跳轉至相應界面

游戲內(nèi)死亡和完成游戲,為一定條件下直接進行頁面跳轉,
在代碼頭加入using UnityEngine.SceneManagement;后即可直接利用下方語句直接跳轉至相應界面

SceneManager.LoadScene("fail");

注意:頁面跳轉需要在unity中building 里將所有SCENE添加到列表中才能實現(xiàn)!!!

發(fā)布

發(fā)布為webgl版本。在building中選擇webgl,根據(jù)提示下載相關組件,并進行項目的轉換。轉換完成后選擇building即可。
注意:項目所有的文件名和路徑名稱必須全部為英文。
注意:使用火狐瀏覽器打開,還需設置其webgl.force-enable為true
security.fileuri.strict_origin_policy為false才可運行。界面如下:

2d橫版游戲demo展示

總結

以上是生活随笔為你收集整理的2D横版游戏Demo试做流程(Unity3D+Visual Studio C#)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。