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
HuntingGameand clickCreate. - In the Project window, create folders named
Scripts,Prefabs, andMaterialsto organize your assets.
Setting Up the Environment
- In the Hierarchy, right-click and select
3D Object > Terrainto create a terrain. - Customize the terrain by using the
Terrain Toolsto 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 > Capsuleto create a player character. - Rename the capsule to
Playerand position it above the terrain. - Add a
Rigidbodycomponent to thePlayerobject for physics-based movement. - Add a
Cameraas a child of thePlayerobject 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.csin theScriptsfolder. - Attach the
PlayerMovementscript to thePlayerobject.
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 Emptyto create a parent object. Name itDeer. - Drag the 3D model into the
Deerobject and reset its transform. - Save the
Deerobject as a prefab by dragging it into thePrefabsfolder.
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.csin theScriptsfolder. - Attach the
AnimalAIscript to theDeerprefab. - Add a
NavMeshAgentcomponent to theDeerprefab. - Set up waypoints in the scene by creating empty GameObjects and positioning them as desired. Assign these waypoints to the
waypointsarray in theAnimalAIscript.
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.csin theScriptsfolder. - Attach the
PlayerShootingscript to thePlayerobject. - Create an impact effect (e.g., a particle system) and assign it to the
impactEffectfield 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.csin theScriptsfolder. - Attach the
ScoreManagerscript to a new empty GameObject namedGameManager. - Create a UI Text element for displaying the score and assign it to the
scoreTextfield in theScoreManagerscript.
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.