Creating a Hunting Game in Unity
In this tutorial, we'll walk through the process of creating a basic hunting game in Unity. This game will include elements such as player movement, animal AI, shooting mechanics, and a scoring system. We will cover:
- Setting up the project and environment
- Creating player controls
- Implementing animal AI
- Adding shooting mechanics
- Setting up a scoring system
Setting Up the Project
Let's start by setting up a new Unity project and creating the environment.
Creating the Project
- Open Unity and create a new 3D project.
- Name your project
HuntingGame
and clickCreate
. - In the Project window, create folders named
Scripts
,Prefabs
, andMaterials
to organize your assets.
Setting Up the Environment
- In the Hierarchy, right-click and select
3D Object > Terrain
to create a terrain. - Customize the terrain by using the
Terrain Tools
to paint textures, add trees, and place grass or other details. - Add a few 3D objects like rocks and trees to make the environment more realistic.
Creating Player Controls
Next, we'll create the player character and implement physics-based movement controls.
Player Character
- In the Hierarchy, right-click and select
3D Object > Capsule
to create a player character. - Rename the capsule to
Player
and position it above the terrain. - Add a
Rigidbody
component to thePlayer
object for physics-based movement. - Add a
Camera
as a child of thePlayer
object to serve as the player's viewpoint.
Player Movement Script
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);
}
}
- Create a new C# script named
PlayerMovement.cs
in theScripts
folder. - Attach the
PlayerMovement
script to thePlayer
object.
Implementing Animal AI
We'll create simple animal AI to roam the environment and react to the player.
Animal Prefab
- Import or create a 3D model for the animal (e.g., a deer).
- Drag the model into the scene and position it on the terrain.
- Right-click the model in the Hierarchy and select
Create Empty
to create a parent object. Name itDeer
. - Drag the 3D model into the
Deer
object and reset its transform. - Save the
Deer
object as a prefab by dragging it into thePrefabs
folder.
Animal AI Script
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);
}
}
}
- Create a new C# script named
AnimalAI.cs
in theScripts
folder. - Attach the
AnimalAI
script to theDeer
prefab. - Add a
NavMeshAgent
component to theDeer
prefab. - Set up waypoints in the scene by creating empty GameObjects and positioning them as desired. Assign these waypoints to the
waypoints
array in theAnimalAI
script.
Adding Shooting Mechanics
We'll implement the ability for the player to shoot at animals.
Shooting Script
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);
}
}
}
- Create a new C# script named
PlayerShooting.cs
in theScripts
folder. - Attach the
PlayerShooting
script to thePlayer
object. - Create an impact effect (e.g., a particle system) and assign it to the
impactEffect
field in the script.
Setting Up a Scoring System
We'll add a simple scoring system to track the player's successful hunts.
Score Manager Script
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;
}
}
- Create a new C# script named
ScoreManager.cs
in theScripts
folder. - Attach the
ScoreManager
script to a new empty GameObject namedGameManager
. - Create a UI Text element for displaying the score and assign it to the
scoreText
field in theScoreManager
script.
Updating the Shooting Script to Track Score
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);
}
}
Conclusion
We've created a basic hunting game in Unity. We set up the project and environment, created player controls with physics-based movement, implemented animal AI, added shooting mechanics, and set up a scoring system. This foundational knowledge can be expanded to include more complex behaviors, additional animals, and refined game mechanics to enhance your hunting game.