问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

Unity打怪兽游戏开发教程:从零开始创建你的冒险之旅

创作时间:
作者:
@小白创作中心

Unity打怪兽游戏开发教程:从零开始创建你的冒险之旅

引用
CSDN
1.
https://blog.csdn.net/2301_81925506/article/details/144154897

Unity引擎以其强大的功能和易用性成为了许多开发者的首选。本文将引导你一步步构建出属于自己的打怪兽游戏。从场景搭建、逻辑编写到最终发布,手把手教你掌握Unity游戏开发的基础知识。

一、准备工作

  1. 安装Unity:首先,你需要从Unity官网(https://unity.com/)下载并安装最新版本的Unity Hub和Unity Editor。Unity对个人开发者提供了免费版本,足以满足我们本次开发的需求。

  2. 设置项目:打开Unity Hub,点击“New”创建一个新项目,选择“3D”模板,为你的项目命名并选择保存位置。

二、游戏场景搭建

  1. 创建地面和添加玩家角色:下载并解压提供的素材包,导入后会自动出现地面、玩家角色以及房屋等物体。

  1. 创建怪兽:在素材包里,导入火自行创建怪兽模型。为怪兽也添加Collider和Rigidbody,但记得调整其属性以适应游戏逻辑,比如设置怪兽为非运动学(Non-Kinematic)。

三、编写游戏逻辑

  1. 玩家控制:新建一个C#脚本(例如move.cs),附加到玩家对象上。在这个脚本中,编写代码以响应玩家输入(如WASD键或方向键)控制角色移动。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Move : MonoBehaviour
{
    public Transform body;
    private int speed = 1;
    private bool isground = true;
    public Rigidbody rig;
    public Transform head;
    public AudioSource footvoice;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float mousex = Input.GetAxis("Mouse X");//获取鼠标水平值
        if (mousex != 0)
        {
            body.Rotate(Vector3.up, 100 * mousex * Time.deltaTime);
        }
        float mousey = Input.GetAxis("Mouse Y");
        if (mousey != 0)
        {
            head.Rotate(Vector3.left, 100 * mousey * Time.deltaTime);
        }
        if (Vector3.Angle(body.forward, head.forward) > 90)
        {
            head.Rotate(Vector3.left, -100 * mousey * Time.deltaTime);
        }
        float horizontal = Input.GetAxis("Horizontal");//定义一个变量用来记录是否按下了W||S
        float vertical = Input.GetAxis("Vertical");//定义一个变量用来记录是否按下了A||D
        if (horizontal != 0 || vertical != 0 && isground == true)//判断 w a s d 是否有一个被按下, 并且在地面上 
        {
            if (footvoice.isPlaying == false)//判断此时是否播放脚步声
            {
                footvoice.Play();
            }
        }
        else
        {
            footvoice.Stop();//停止播放脚步声
        }
        if (Input.GetKey(KeyCode.Space) && isground == true)
        {
            rig.AddForce(Vector3.up * 10);
        }
        if (Input.GetKey(KeyCode.LeftShift))
        {
            speed = 10;
        }
        else
        {
            speed = 5;
        }
        if (Input.GetKey(KeyCode.A))
        {
            body.Translate(Vector3.left * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            body.Translate(Vector3.right * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.W))
        {
            body.Translate(Vector3.forward * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.S))
        {
            body.Translate(Vector3.back * Time.deltaTime * speed);
        }
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == "ground")
        {
            isground = true;
        }
    }

    private void OnCollisionExit(Collision collision)
    {
        if (collision.collider.tag == "ground")
        {
            isground = false;
        }
    }
}
  1. 怪兽行为:为怪兽创建一个脚本(例如enmary.cs),实现简单的巡逻或追逐玩家逻辑。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class enmary : MonoBehaviour
{
    public Text text;
    private int hp = 5;
    public slid a;

    // Start is called before the first frame update
    void Start()
    {
        a.SetMaxHealth(hp);
    }

    // Update is called once per frame
    void Update()
    {
    }

    //private void OnCollisionEnter(Collision collision)
    //{
    //    if (collision.collider.tag=="bullet") {
    //        hp--;
    //        if (hp==0) {
    //            Destroy(gameObject);
    //        }
    //    }
    //}

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "bullet")
        {
            hp--;
            a.SetCurrentHealth(hp);
            text.text = "怪物血量" + hp;
            if (hp == 0)
            {
                Destroy(gameObject);
            }
        }
    }
}
  1. 子弹发射与完善:为枪支创建子弹,并设计子弹发射脚本(如gunctrll.cs)并添加子弹发射间隔,子弹发射声音,枪火,换弹等代码,实现玩家攻击怪兽的逻辑。
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;
using UnityEngine.UI;

public class gunctrl1 : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform firepoint;//枪口火焰生成的位置
    public Transform bulletpoint;//子弹生成的位置
    public GameObject bulletpreb;//子弹的预制体
    public GameObject firepreb;//火焰的预制体
    public AudioSource gunshot;//控制开火声音
    public AudioClip clip;//枪声音频
    private float cd = 0.2f;//子弹之间的发射间隔
    private float timer = 0;//计算实际子弹发射时间的间隔
    public Text bulletText;//子弹UI显示
    public int bulletCount = 5;//子弹个数

    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        timer = timer + Time.deltaTime;//计算实际过了多少秒
        if (Input.GetMouseButtonDown(0) && timer > cd && bulletCount > 0)
        {
            timer = 0;
            Instantiate(bulletpreb, bulletpoint.position, bulletpoint.rotation);//子弹生成
            Instantiate(firepreb, firepoint.position, firepoint.rotation);//火焰生成
            gunshot.PlayOneShot(clip);
            bulletCount--;
            bulletText.text = "子弹个数:" + bulletCount;
        }
        if (bulletCount == 0 && timer == 0)
        {
            GetComponent<Animator>().SetTrigger("Reload");
        }
        if (bulletCount == 0 && timer != 0)
        {
            Reload();
        }
        void Reload()
        {
            if (timer > 1.5)
                bulletCount = 5;
            bulletText.text = "子弹个数:" + bulletCount;
        }
    }
}

四、游戏界面与UI

  1. 添加得分与掉血系统:在场景中创建两个UI Text元素来显示得分与掉血。并在玩家击败怪兽时更新分数与怪兽掉血。

  2. 给怪兽设计血条:当子弹打到怪兽时间,血条会出现变化。

  3. 给怪兽设计行走路线:添加UI插件(AI Navigation),设计几个空对象(如A,B,C,D)作为怪兽行走的路线,并与怪兽脚本关联。

五、测试与优化

  1. 运行测试:不断运行游戏,测试各项功能是否正常,包括玩家控制、怪兽行为、碰撞检测等。

  2. 性能优化:检查游戏性能,优化图形设置、减少不必要的渲染和物理计算,确保游戏在不同设备上都能流畅运行。

六、发布与分享

  1. 构建游戏:在Unity中,通过“File” -> “Build Settings”选择目标平台(如PC、Mac、Android或iOS),进行游戏构建。

  2. 分享作品:将你的游戏分享给朋友、上传到游戏平台或社交媒体,让更多人体验到你的创作成果。

通过以上步骤,你已经成功创建了一个基础的打怪兽游戏。这只是一个起点,你可以根据自己的创意不断扩展游戏内容,添加更多功能、关卡和故事情节,让游戏更加丰富有趣。Unity的强大之处在于其无限的可能性,希望你能在这个平台上创造出属于自己的游戏世界!

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号