這篇文章主要為大家展示了“Unity如何實現射擊小游戲”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“Unity如何實現射擊小游戲”這篇文章吧。
這款小游戲只用了兩個UI界面,一個是菜單界面,另一個是戰斗界面
菜單界面有三種模式,分別是一般、困難和地獄
戰斗界面就是很簡單的從兩邊刷野怪,然后主角開槍打死他們
UI搭建很簡單,只有一張背景圖使用Image,加上一個Text文本輸入框,然后擺放一下位置即可!
菜單界面如下

戰斗畫面演示

這款橫版2D射擊小游戲,核心部分只需要 兩個腳本 就可以完成,一學就會,一起來看一下代碼吧!
一個掛載到玩家身上的腳本PlayerMove,用于控制玩家的左右移動和跳躍
還有兩種開槍的方法,分別是穿透和重擊!當碰到怪物的時候就會掉血
當玩家血量低于0 的時候就跳出 游戲結束 的畫面,非常簡單清晰的邏輯!
代碼如下,一個沒有多少行,代碼內容也是Unity最基礎的知識點,相信稍微懂一點的人也都能看懂
PlayerMove代碼如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMove : MonoBehaviour {
/// <summary>
/// 生命值
/// </summary>
public int HP;
public Slider hpUI;
/// <summary>
/// 動畫組建
/// </summary>
public Animator _animator;
/// <summary>
/// 擊殺
/// </summary>
public static int jisha;
public Text jishaUI;
public Text jishaUIEnd;
/// <summary>
/// 是否跳躍
/// </summary>
public bool isJump;
/// <summary>
/// 游戲結束UI
/// </summary>
public GameObject ui;
// Use this for initialization
void Start ()
{
Time.timeScale = 1;
jisha = 0;
hpUI.maxValue = HP;
hpUI.value = HP;
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown (KeyCode.Escape))
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Menu");
}
if (HP <= 0)
{
HP = 0;
}
jishaUI.text = "擊殺:" + jisha;
jishaUIEnd.text = "擊殺:" + jisha;
if (HP == 0)
{
ui.SetActive(true);
Time.timeScale = 0;
}
hpUI.transform.position = Camera.main.WorldToScreenPoint(transform.position + new Vector3(0, 3, 0));
hpUI.value = HP;
if (Input.GetKey (KeyCode.D))
{
transform.localEulerAngles = new Vector3(0, 0, 0);
transform.Translate(Vector3.right * Time.deltaTime * 7);
_animator.SetBool("run", true);
}
else if (Input.GetKey(KeyCode.A))
{
transform.localEulerAngles = new Vector3(0, 180, 0);
transform.Translate(Vector3.right * Time.deltaTime * 7);
_animator.SetBool("run", true);
}
else
{
_animator.SetBool("run", false);
}
if (Input.GetKeyDown (KeyCode.Space) && !isJump)
{
GetComponent<Rigidbody2D>().velocity = new Vector2(0, 30);
}
if (isJump)
{
_animator.SetBool("Jump", true);
}
else
{
_animator.SetBool("Jump", false);
}
if (Input.GetKeyDown (KeyCode.J))
{
GameObject n = Instantiate(Resources.Load("a1"), transform.GetChild(0).position, transform.GetChild(0).rotation) as GameObject;
n.name = "a1";
Destroy(n, 2);
}
if (Input.GetKeyDown(KeyCode.K))
{
GameObject n = Instantiate(Resources.Load("a2"), transform.GetChild(0).position, transform.GetChild(0).rotation) as GameObject;
n.name = "a2";
Destroy(n, 2);
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.name == "0")
{
isJump = false;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.name == "0")
{
isJump = true;
}
}
public void ReturnMenu()
{
UnityEngine.SceneManagement.SceneManager.LoadScene("Menu");
}
}還有一個Monster腳本,用于顯示怪物的生命、速度和攻擊力等
還有受到玩家打出的子彈就會掉血,很簡單的方法就可以實現!
完整代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Monster : MonoBehaviour {
/// <summary>
/// 生命
/// </summary>
public int HP;
/// <summary>
/// 速度
/// </summary>
public float speed;
/// <summary>
/// 攻擊力
/// </summary>
public int att;
public Slider hpUI;
// Use this for initialization
void Start ()
{
speed = Random.RandomRange(speed - 2, speed + 2);
Destroy(gameObject, 10);
GameObject n = Instantiate(Resources.Load("HP")) as GameObject;
n.transform.SetParent(GameObject.Find("HPShow").transform);
n.transform.localScale = Vector3.one;
hpUI = n.GetComponent<Slider>();
hpUI.maxValue = HP;
hpUI.value = HP;
}
// Update is called once per frame
void Update ()
{
if (HP <= 0)
{
HP = 0;
PlayerMove.jisha++;
Destroy(gameObject);
}
hpUI.transform.position = Camera.main.WorldToScreenPoint(transform.position + new Vector3 (0, 6, 0));
hpUI.value = HP;
transform.Translate(Vector3.right * Time.deltaTime * speed);
}
private void OnTriggerEnter2D(Collider2D collision)
{
//撞到玩家生命值
if (collision.name == "Player")
{
collision.GetComponent<PlayerMove>().HP -= Random.RandomRange(att - 2, att + 2);
Destroy(gameObject);
}
//紅色子彈傷害低,能夠連續穿透射擊多個目標
if (collision.name == "a1")
{
HP -= Random.RandomRange(8, 15);
}
//紅色子彈傷害高,只能擊中一個目標
if (collision.name == "a2")
{
HP -= Random.RandomRange(20, 40);
Destroy(collision.gameObject);
}
}
private void OnDestroy()
{
Destroy(hpUI.gameObject);
}
}這個小游戲只需要上面兩個腳本差不多就算是完成了,然后還可以打包出來到電腦上面玩!
打包這個游戲也很簡單,只需要在Build下將下面幾個場景添加進去直接Build就好啦!

打包出來的文件夾是這個樣子的,然后點擊.exe文件就可以直接在電腦上玩了!

以上是“Unity如何實現射擊小游戲”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。