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

  1. Open Unity and create a new 3D project.
  2. Name your project HuntingGame and click Create.
  3. In the Project window, create folders named Scripts, Prefabs, and Materials to organize your assets.

Setting Up the Environment

  1. In the Hierarchy, right-click and select 3D Object > Terrain to create a terrain.
  2. Customize the terrain by using the Terrain Tools to paint textures, add trees, and place grass or other details.
  3. 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

  1. In the Hierarchy, right-click and select 3D Object > Capsule to create a player character.
  2. Rename the capsule to Player and position it above the terrain.
  3. Add a Rigidbody component to the Player object for physics-based movement.
  4. Add a Camera as a child of the Player 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);
    }
}
  1. Create a new C# script named PlayerMovement.cs in the Scripts folder.
  2. Attach the PlayerMovement script to the Player object.

Implementing Animal AI

We'll create simple animal AI to roam the environment and react to the player.

Animal Prefab

  1. Import or create a 3D model for the animal (e.g., a deer).
  2. Drag the model into the scene and position it on the terrain.
  3. Right-click the model in the Hierarchy and select Create Empty to create a parent object. Name it Deer.
  4. Drag the 3D model into the Deer object and reset its transform.
  5. Save the Deer object as a prefab by dragging it into the Prefabs 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);
        }
    }
}
  1. Create a new C# script named AnimalAI.cs in the Scripts folder.
  2. Attach the AnimalAI script to the Deer prefab.
  3. Add a NavMeshAgent component to the Deer prefab.
  4. Set up waypoints in the scene by creating empty GameObjects and positioning them as desired. Assign these waypoints to the waypoints array in the AnimalAI 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);
        }
    }
}
  1. Create a new C# script named PlayerShooting.cs in the Scripts folder.
  2. Attach the PlayerShooting script to the Player object.
  3. 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;
    }
}
  1. Create a new C# script named ScoreManager.cs in the Scripts folder.
  2. Attach the ScoreManager script to a new empty GameObject named GameManager.
  3. Create a UI Text element for displaying the score and assign it to the scoreText field in the ScoreManager 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.