Unity Basic Course: Creating Effects on Hit Areas

This course covers how to generate different effects depending on the parts hit by a bullet in Unity. This is highly effective in enhancing the immersion of the game and works in conjunction with various elements like enemy health, enemy animations, and the particle system.

1. Course Objectives

The objectives of this course are as follows:

  • Learn how to detect collisions for each part of a 3D model in Unity
  • Understand how to set up the effects to display when hit in each part
  • Utilize the particle system to implement effects
  • Write scripts to utilize the functionality in an actual game

2. Prerequisites

Before starting the course, please prepare the following:

  • Unity installed
  • Basic knowledge of C# programming
  • 3D model source or example model
  • Particle assets (if available)

3. Basic Unity Project Setup

After launching Unity, create a new project. Select the 3D template and add simple game objects to set up the environment. These game objects may include ground, enemy characters, and player characters.

3.1 Importing 3D Models

To import a 3D model into Unity:

  1. Right-click in the Project View → Select Import New Asset
  2. Select the 3D model file (.fbx, .obj, etc.) to import
  3. Drag the imported model into the scene view

3.2 Setting Up Characters and the Scene

Add basic terrain to the scene and place the enemy characters. Add Rigidbody and Collider components to enemy characters for accurate collision handling.

4. Collision Handling and Effect Generation

Now we need to set up collision detection to generate effects when hit in each part. We will use the Raycasting technique for this.

4.1 Understanding Raycasting

Raycasting is a technique that shoots a ‘ray’ in a given direction to detect contacted objects. It is mainly used in shooting games to detect the position of enemies based on the shot direction.

4.2 Writing Collision Detection Script

Create a C# script to implement collision detection. The following code detects which part was hit when the player’s bullet hits an enemy:

        using UnityEngine;

        public class Gun : MonoBehaviour
        {
            public float range = 100f;
            public Transform gunEnd;

            void Update()
            {
                if (Input.GetButtonDown("Fire1"))
                {
                    Shoot();
                }
            }

            void Shoot()
            {
                RaycastHit hit;
                if (Physics.Raycast(gunEnd.position, gunEnd.forward, out hit, range))
                {
                    Debug.Log("Hit: " + hit.transform.name);
                    ApplyHitEffect(hit);
                }
            }

            void ApplyHitEffect(RaycastHit hit)
            {
                // Handle effects based on the part of the enemy hit
                if (hit.collider.CompareTag("Enemy"))
                {
                    string hitPart = hit.collider.name; // Name of the hit part
                    switch (hitPart)
                    {
                        case "Head":
                            // Effect when hit in the head
                            Debug.Log("Head shot!");
                            break;
                        case "Body":
                            // Effect when hit in the body
                            Debug.Log("Body hit!");
                            break;
                        case "Leg":
                            // Effect when hit in the leg
                            Debug.Log("Leg hit!");
                            break;
                        default:
                            break;
                    }
                }
            }
        }
        

4.3 Managing Enemy Health

You can set the enemy’s health to add damage effects based on the parts hit. Write a health management script to calculate the damage received by the enemy:

        using UnityEngine;

        public class Enemy : MonoBehaviour
        {
            public float health = 100f;

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

            void Die()
            {
                // Handle enemy death
                Debug.Log("Enemy died!");
                Destroy(gameObject);
            }
        }
        

5. Visualizing Effects Based on Parts

To provide visual effects when hit in a part, we utilize the particle system. You can create different particle effects depending on the hit part.

5.1 Adding Particle System

Use Unity's Particle System to create effects:

  1. Right-click in the Hierarchy window → Effects → Select Particle System
  2. Set the Position of the newly created Particle System from the script.
  3. Adjust the settings of the Particle System to create the desired effect.
  4. Activate this Particle System when hitting the enemy character.
  5. Here’s an example of a script that switches effects based on the parts:
        void ApplyHitEffect(RaycastHit hit)
        {
            if (hit.collider.CompareTag("Enemy"))
            {
                string hitPart = hit.collider.name; // Name of the hit part
                switch (hitPart)
                {
                    case "Head":
                        // Effect when hit in the head
                        Instantiate(headShotEffect, hit.point, Quaternion.LookRotation(hit.normal));
                        break;
                    case "Body":
                        // Effect when hit in the body
                        Instantiate(bodyHitEffect, hit.point, Quaternion.LookRotation(hit.normal));
                        break;
                    case "Leg":
                        // Effect when hit in the leg
                        Instantiate(legHitEffect, hit.point, Quaternion.LookRotation(hit.normal));
                        break;
                    default:
                        break;
                }
            }
        }
        

6. Conclusion and Optimization

The functionality to generate effects based on the parts hit by a bullet is now fundamentally implemented. There are areas that can be further improved and optimized:

  • Add part-specific animations
  • Optimize effects performance
  • Add various weapons and corresponding effect changes

7. Conclusion

Through this course, you have learned the basics for generating effects based on the parts hit by a bullet in Unity. By implementing several essential elements such as collision detection, particle effects, and enemy health management, you have strengthened your foundational skills in Unity.

I hope this course has been helpful in learning Unity, and I plan to cover various topics in the future, so I appreciate your interest!

8. References

You can learn more advanced topics through the official Unity documentation and various tutorials: