在 Unity 中创建狩猎游戏
在本教程中,我们将介绍在 Unity 中创建基本狩猎游戏的过程。此游戏将包括玩家移动、动物 AI、射击机制和计分系统等元素。我们将介绍:
- 设置项目和环境
- 创建播放器控件
- 实现动物人工智能
- 添加射击机制
- 建立评分系统
设置项目
让我们首先建立一个新的 Unity 项目并创建环境。
创建项目
- 打开Unity并创建一个新的3D项目。
- 将您的项目命名为
HuntingGame
,然后单击Create
。 - 在项目窗口中,创建名为
Scripts
、Prefabs
和Materials
的文件夹来组织您的资产。
设置环境
- 在层次结构中,右键单击并选择
3D Object > Terrain
来创建地形。 - 使用
地形工具
绘制纹理、添加树木以及放置草地或其他细节来定制地形。 - 添加一些 3D 物体,如岩石和树木,使环境更加逼真。
创建播放器控件
接下来,我们将创建玩家角色并实现基于物理的运动控制。
玩家角色
- 在层次结构中,右键单击并选择
3D Object > Capsule
来创建玩家角色。 - 将胶囊重命名为
Player
并将其放置在地形上方。 - 向
Player
对象添加Rigidbody
组件,以实现基于物理的运动。 - 添加
Camera
作为Player
对象的子对象,以作为玩家的视点。
玩家移动脚本
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float moveSpeed = 5f;
public float rotationSpeed = 100f;
private Rigidbody rb;
void Start()
{
rb = GetComponent();
rb.freezeRotation = true;
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = transform.forward * moveVertical * moveSpeed * Time.deltaTime;
rb.MovePosition(rb.position + movement);
float rotation = Input.GetAxis("Mouse X") * rotationSpeed * Time.deltaTime;
Quaternion turnRotation = Quaternion.Euler(0f, rotation, 0f);
rb.MoveRotation(rb.rotation * turnRotation);
}
}
- 在
Scripts
文件夹中创建一个名为PlayerMovement.cs
的新 C# 脚本。 - 将
PlayerMovement
脚本附加到Player
对象。
实现动物人工智能
我们将创建简单的动物人工智能来在环境中漫游并对玩家做出反应。
动物预制件
- 导入或创建动物(例如鹿)的 3D 模型。
- 将模型拖入场景并将其放置在地形上。
- 在层次结构中右键单击模型,然后选择
Create Empty
以创建父对象。将其命名为Deer
。 - 将 3D 模型拖入
Deer
对象并重置其变换。 - 将
Deer
对象拖入Prefabs
文件夹,将其保存为预制件。
动物人工智能脚本
using UnityEngine;
using UnityEngine.AI;
public class AnimalAI : MonoBehaviour
{
public Transform[] waypoints;
private NavMeshAgent agent;
private int currentWaypoint = 0;
void Start()
{
agent = GetComponent();
agent.SetDestination(waypoints[currentWaypoint].position);
}
void Update()
{
if (agent.remainingDistance < agent.stoppingDistance)
{
currentWaypoint = (currentWaypoint + 1) % waypoints.Length;
agent.SetDestination(waypoints[currentWaypoint].position);
}
}
}
- 在
Scripts
文件夹中创建一个名为AnimalAI.cs
的新 C# 脚本。 - 将
AnimalAI
脚本附加到Deer
预制件。 - 将
NavMeshAgent
组件添加到Deer
预制件。 - 通过创建空的游戏对象并根据需要定位它们来设置场景中的航点。将这些航点分配给
AnimalAI
脚本中的waypoints
数组。
添加射击机制
我们将实现玩家射击动物的能力。
拍摄脚本
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public Camera playerCamera;
public float range = 100f;
public GameObject impactEffect;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, range))
{
AnimalAI animal = hit.transform.GetComponent();
if (animal != null)
{
Destroy(hit.transform.gameObject);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
}
- 在
Scripts
文件夹中创建一个名为PlayerShooting.cs
的新 C# 脚本。 - 将
PlayerShooting
脚本附加到Player
对象。 - 创建一个冲击效果(例如,粒子系统)并将其分配给脚本中的
impactEffect
字段。
建立评分系统
我们将添加一个简单的计分系统来追踪玩家的成功狩猎。
分数管理脚本
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public static int score = 0;
public Text scoreText;
void Update()
{
scoreText.text = "Score: " + score.ToString();
}
public static void AddScore(int points)
{
score += points;
}
}
- 在
Scripts
文件夹中创建一个名为ScoreManager.cs
的新 C# 脚本。 - 将
ScoreManager
脚本附加到名为GameManager
的新空 GameObject。 - 创建一个用于显示分数的 UI Text 元素,并将其分配给
ScoreManager
脚本中的scoreText
字段。
更新拍摄脚本以追踪得分
void Shoot()
{
RaycastHit hit;
if (Physics.Raycast(playerCamera.transform.position, playerCamera.transform.forward, out hit, range))
{
AnimalAI animal = hit.transform.GetComponent();
if (animal != null)
{
Destroy(hit.transform.gameObject);
ScoreManager.AddScore(10);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 2f);
}
}
结论
我们在 Unity 中创建了一个基本的狩猎游戏。我们设置了项目和环境,创建了基于物理运动的玩家控件,实现了动物 AI,添加了射击机制,并设置了计分系统。这些基础知识可以扩展为包括更复杂的行为、更多动物和精致的游戏机制,以增强您的狩猎游戏。