Basic Unity Course: Shooting 2 – Shooting Without Bullets

Hello! In this tutorial, we will learn how to implement the interesting concept of “shooting without bullets” in Unity. The concept of shooting without bullets can act as an element that provides a more creative and new gameplay experience, going beyond the traditional shooting game’s bullet firing mechanism.

In this tutorial, we will cover how players interact with enemies without using bullets, how enemies recognize the player’s position, and how players attack. We will also explain the necessary C# scripts and setup methods to implement these elements in detail.

1. Basic Concept of Shooting Without Bullets

Shooting without bullets is a way for players to hit enemies within a certain range. This can be implemented in the form of melee attacks or area attacks that can inflict damage on enemies every time the player presses the attack button. This method can create various gameplay mechanics instead of simple bullet firing.

For example, consider a system where the player uses an attack like a laser that hits all enemies in the direction it points. This requires players to strategize their attacks, while enemies may take actions to evade. This mechanism offers a unique gaming experience.

2. Setting Up the Unity Project

The first step is to set up a Unity project. Launch Unity and create a new 3D project. After setting the project name and save location, create the project.

2.1 Configuring the Scene

The scene needs a player character, enemies, and a UI to display the attack range. Follow the steps below to configure the scene:

  • Creating the Player Character: Create a 3D model or replace it with Unity’s basic cube object to create the player character.
  • Placing Enemies: Place several enemy models in the scene to interact with the player from various positions.
  • Adding UI Elements: Add a UI Canvas to visually represent the attack range and create a UI object to indicate the attack radius.

3. Writing the Player Script

Now, we will write a player script to allow the player to hit enemies when attacking. Create a C# script and name it ‘PlayerController’.

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float attackRange = 5f;       // Attack range
    public LayerMask enemyLayer;        // Enemy layer

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) // Attack with the space key
        {
            Attack();
        }
    }

    void Attack()
    {
        Collider[] hitEnemies = Physics.OverlapSphere(transform.position, attackRange, enemyLayer);
        foreach (Collider enemy in hitEnemies)
        {
            // Add logic to hit the enemy here
            Debug.Log("Hit " + enemy.name);
        }
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, attackRange); // Visualize attack range
    }
}

The above code allows the player to detect enemies within the specified attack range when the space bar is pressed. It uses the Physics.OverlapSphere function to detect enemies around the player and logs a message every time an enemy is detected.

4. Writing the Enemy Script

Now, let’s write a script that defines the behavior of the enemy when receiving damage from the player’s attack. Create a new script named ‘EnemyController’.

using UnityEngine;

public class EnemyController : MonoBehaviour
{
    public int health = 100; // Enemy health

    public void TakeDamage(int damage)
    {
        health -= damage;
        if (health <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        Debug.Log(name + " has died!");
        Destroy(gameObject); // Remove enemy object
    }
}

In the above code, the enemy has health and takes damage from the player, reducing their health. If health falls to 0 or below, the enemy dies, and the game object is destroyed.

Now let’s add the logic for the enemy taking damage to the Attack() method in the PlayerController.

void Attack()
{
    Collider[] hitEnemies = Physics.OverlapSphere(transform.position, attackRange, enemyLayer);
    foreach (Collider enemy in hitEnemies)
    {
        EnemyController enemyController = enemy.GetComponent<EnemyController>();
        if (enemyController != null)
        {
            enemyController.TakeDamage(10); // Specify damage amount
            Debug.Log("Hit " + enemy.name);
        }
    }
}

5. Testing

After writing all the scripts, press the play button in the Unity editor to test. Check if the nearby enemies take damage when you press the space bar. When an enemy dies, a related message will be output in the log.

During this process, you can adjust the attack range and modify the enemy’s health for various gameplay adjustments.

6. Additional Expansion Ideas

In this tutorial, we implemented a simple form of shooting without bullets. Here are some ideas for expanding this:

  • Adding Different Types of Attacks: Enhance the variety of the game by adding various attack methods that players can use.
  • Changing Enemy Behavior Patterns: Add behaviors where enemies not only chase the player but also evade or counterattack.
  • Items and Power-ups: Implement mechanisms that increase attack power or range by adding items or power-ups that players can use.

7. Conclusion

The shooting without bullets game mechanism provides players with a new experience and enhances the game’s strategy. Having learned the basic implementation methods in this tutorial, it is up to you to advance this system with more features and elements.

The process of creating games with Unity is challenging but simultaneously a creative and enjoyable journey. I hope this will be of great help on your path ahead. Thank you!