Skip to content Skip to sidebar Skip to footer

Widget HTML #1

Complete 3D FPS Game in Unity C# For Beginners 2024



Complete 3D FPS Game in Unity C# For Beginners 2024

Embark on an exhilarating journey into the realm of game development with our 3D FPS (First-Person Shooter) course tailored for beginners in Unity.

Enroll Now

Creating a 3D First-Person Shooter (FPS) game in Unity is an excellent project for beginners who want to dive into game development. Unity provides a user-friendly interface and powerful tools to bring your game ideas to life. In this guide, we'll walk you through the basic steps to create a simple FPS game using C# scripting in Unity.

Setting Up Your Project

  1. Install Unity Hub and Unity Editor: If you haven't already, download and install Unity Hub from the Unity website. Use Unity Hub to install the latest version of the Unity Editor.

  2. Create a New Project:

    • Open Unity Hub, click on the "New" button.
    • Select the "3D" template.
    • Name your project (e.g., "MyFPSGame") and choose a location to save it.
    • Click "Create" to set up the new project.

Setting Up the Scene

  1. Create the Environment:

    • In the Hierarchy window, right-click and create a new 3D Object (e.g., Terrain).
    • Use the Terrain tools to shape your landscape, add textures, and make it look like a playable area.
  2. Add a Player Character:

    • In the Asset Store or Package Manager, search for and import the "Standard Assets" package which includes useful prefabs and scripts.
    • Drag the "FPSController" prefab from the Standard Assets into the Scene. This prefab includes a pre-configured player character with a camera and basic movement controls.
  3. Set Up the Main Camera:

    • Ensure the Main Camera is attached to the player character. The FPSController prefab typically handles this automatically.

Scripting the FPS Mechanics

  1. Creating a Shooting Script:

    • In the Project window, create a new folder named "Scripts".
    • Inside the "Scripts" folder, create a new C# script named "PlayerShooting".
    • Double-click the script to open it in your code editor (e.g., Visual Studio).
  2. Implementing the Shooting Logic:

    • Open PlayerShooting.cs and write the following code:
    csharp
    using UnityEngine; public class PlayerShooting : MonoBehaviour { public float damage = 10f; public float range = 100f; public Camera fpsCam; public ParticleSystem muzzleFlash; public GameObject impactEffect; void Update() { if (Input.GetButtonDown("Fire1")) { Shoot(); } } void Shoot() { muzzleFlash.Play(); RaycastHit hit; if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range)) { Debug.Log(hit.transform.name); Target target = hit.transform.GetComponent<Target>(); if (target != null) { target.TakeDamage(damage); } GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)); Destroy(impactGO, 2f); } } }
    • Attach the PlayerShooting script to the FPSController in the Inspector.
  3. Creating a Target Script:

    csharp
    using UnityEngine; public class Target : MonoBehaviour { public float health = 50f; public void TakeDamage(float amount) { health -= amount; if (health <= 0f) { Die(); } } void Die() { Destroy(gameObject); } }
    • Attach the Target script to any GameObject you want to be destructible (e.g., enemy, destructible crates).

Adding Effects

  1. Muzzle Flash:

    • Import a particle effect for the muzzle flash.
    • In the Inspector, assign the muzzleFlash Particle System to the corresponding field in the PlayerShooting script.
  2. Impact Effect:

    • Create a simple particle effect or use a prefab for bullet impact.
    • Assign the impactEffect GameObject to the corresponding field in the PlayerShooting script.

Adding Enemy AI

  1. Basic Enemy Movement:

    • In the "Scripts" folder, create a new C# script named "EnemyAI".
    • Open EnemyAI.cs and write the following code:
    csharp
    using UnityEngine; using UnityEngine.AI; public class EnemyAI : MonoBehaviour { public Transform player; public float lookRadius = 10f; NavMeshAgent agent; void Start() { agent = GetComponent<NavMeshAgent>(); } void Update() { float distance = Vector3.Distance(player.position, transform.position); if (distance <= lookRadius) { agent.SetDestination(player.position); if (distance <= agent.stoppingDistance) { FaceTarget(); // Attack logic here } } } void FaceTarget() { Vector3 direction = (player.position - transform.position).normalized; Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z)); transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f); } void OnDrawGizmosSelected() { Gizmos.color = Color.red; Gizmos.DrawWireSphere(transform.position, lookRadius); } }
    • Attach the EnemyAI script to an enemy GameObject.
    • Ensure the enemy has a NavMesh Agent component.
    • Assign the player (FPSController) to the player field in the Inspector.
  2. Setting Up NavMesh:

    • In the Hierarchy window, select the Terrain or ground GameObject.
    • Go to the Navigation window (Window -> AI -> Navigation).
    • Mark the Terrain as "Walkable" and click "Bake" to generate the NavMesh.

Final Touches

  1. UI Elements:

    • Add a crosshair by creating a simple UI Canvas.
    • Add a crosshair image to the center of the screen.
  2. Sound Effects:

    • Import audio clips for shooting and impact sounds.
    • Add an AudioSource component to the player and set it up to play the shooting sound.
  3. Polishing:

    • Fine-tune the player movement, shooting mechanics, and enemy AI for a better gameplay experience.
    • Add more levels, obstacles, and different types of enemies to make the game more engaging.

Testing and Building

  1. Testing:

    • Regularly test your game within the Unity Editor to catch bugs and ensure smooth gameplay.
    • Use the Unity Profiler to optimize performance.
  2. Building the Game:

    • Once satisfied, go to File -> Build Settings.
    • Add the current scene to the build.
    • Choose your target platform (e.g., PC, Mac, Linux).
    • Click "Build and Run" to create your game executable.

Conclusion

Creating a 3D FPS game in Unity is a fantastic way to learn the basics of game development. This guide covered the essentials: setting up the environment, scripting player and enemy behaviors, adding effects, and finalizing your game. Unity’s extensive documentation and community support make it easier to expand on this foundation and create more complex and polished games in the future. Keep experimenting, learning, and most importantly, have fun creating your game!