Unity打怪兽游戏开发教程:从零开始创建你的冒险之旅
创作时间:
作者:
@小白创作中心
Unity打怪兽游戏开发教程:从零开始创建你的冒险之旅
引用
CSDN
1.
https://blog.csdn.net/2301_81925506/article/details/144154897
Unity引擎以其强大的功能和易用性成为了许多开发者的首选。本文将引导你一步步构建出属于自己的打怪兽游戏。从场景搭建、逻辑编写到最终发布,手把手教你掌握Unity游戏开发的基础知识。
一、准备工作
安装Unity:首先,你需要从Unity官网(https://unity.com/)下载并安装最新版本的Unity Hub和Unity Editor。Unity对个人开发者提供了免费版本,足以满足我们本次开发的需求。
设置项目:打开Unity Hub,点击“New”创建一个新项目,选择“3D”模板,为你的项目命名并选择保存位置。
二、游戏场景搭建
- 创建地面和添加玩家角色:下载并解压提供的素材包,导入后会自动出现地面、玩家角色以及房屋等物体。
- 创建怪兽:在素材包里,导入火自行创建怪兽模型。为怪兽也添加Collider和Rigidbody,但记得调整其属性以适应游戏逻辑,比如设置怪兽为非运动学(Non-Kinematic)。
三、编写游戏逻辑
- 玩家控制:新建一个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;
}
}
}
- 怪兽行为:为怪兽创建一个脚本(例如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);
}
}
}
}
- 子弹发射与完善:为枪支创建子弹,并设计子弹发射脚本(如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
添加得分与掉血系统:在场景中创建两个UI Text元素来显示得分与掉血。并在玩家击败怪兽时更新分数与怪兽掉血。
给怪兽设计血条:当子弹打到怪兽时间,血条会出现变化。
给怪兽设计行走路线:添加UI插件(AI Navigation),设计几个空对象(如A,B,C,D)作为怪兽行走的路线,并与怪兽脚本关联。
五、测试与优化
运行测试:不断运行游戏,测试各项功能是否正常,包括玩家控制、怪兽行为、碰撞检测等。
性能优化:检查游戏性能,优化图形设置、减少不必要的渲染和物理计算,确保游戏在不同设备上都能流畅运行。
六、发布与分享
构建游戏:在Unity中,通过“File” -> “Build Settings”选择目标平台(如PC、Mac、Android或iOS),进行游戏构建。
分享作品:将你的游戏分享给朋友、上传到游戏平台或社交媒体,让更多人体验到你的创作成果。
通过以上步骤,你已经成功创建了一个基础的打怪兽游戏。这只是一个起点,你可以根据自己的创意不断扩展游戏内容,添加更多功能、关卡和故事情节,让游戏更加丰富有趣。Unity的强大之处在于其无限的可能性,希望你能在这个平台上创造出属于自己的游戏世界!
热门推荐
合欢树种子几月份种?春季3-5月最佳
提高劳动效率的有效方法
驾照可以终身免检了?交警提醒:就这3个条件去了就给换!
生菜无土栽培技术与管理方法:水培营养液的配制与管理的深度解析
充气床垫的优点、注意事项以及保养方法
香港的利得税和资本利得税:了解税务制度和优惠政策
苯磺酸氨氯地平片与尼莫地平对比
瓜蒌种植技术及芋头加工解决方案
李阳家暴事件引发社会广泛关注:家庭暴力何时了
不同类型机油的更换时间有何区别?
為何會有討好型人格?
一文搞懂常见的导数公式及应用场景
2025研考今日开考:考研人数"跳水","上岸"变容易了?
大蒜油的功效与作用是什么
海外小学数学分数除法:概念、口诀、练习题PDF
晚上喝酸奶有什么好处和坏处
DB23T 3772-2024 寒地高油酸大豆种质资源筛选鉴定技术规程
血压高?高血压?医生教你如何判断和应对!
关于GDP及其相关概念的理解
金针虫:地下害虫的防治指南
手腕痛怎么治疗
利用ollama + RAGFlow部署千问大模型构建个人知识库AI智能体应用
高血压患者的自我管理之道
如何规划和设计一个家庭小型温泉池?
农村经济的挑战与机遇
伊春市十大旅游景点
母亲脂溢性脱发会遗传吗(脂溢性脱发会遗传吗)
卫生间装修搭配指南:打造高贵冷艳的卫浴空间
插头插座新国标GB 1002-2024 主要有这21点技术变化!
新疆大盘鸡的美味做法与历史渊源