first person shooter in csharp

To create a first person shooter game in C#, you can use Unity, a popular game engine that supports C# programming language. Here are the basic steps to create a first person shooter game in Unity:

  1. Set up a new project in Unity and import the necessary assets, such as 3D models, textures, and sounds.

  2. Create a new scene and add a camera as the player’s viewpoint.

  3. Write C# scripts to handle player movement, shooting, and interaction with the environment. For example, you can use the Input class and the Transform component to move the player character, and the Raycast function to detect collisions between the player’s bullets and the enemies.

main.cs
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float moveSpeed = 5.0f;
    public float jumpSpeed = 10.0f;
    public Transform groundCheck;
    public LayerMask groundLayer;

    private Rigidbody rb;
    private bool isGrounded;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");

        Vector3 moveDirection = new Vector3(horizontalInput, 0.0f, verticalInput);
        moveDirection = transform.TransformDirection(moveDirection);
        moveDirection *= moveSpeed;

        rb.MovePosition(rb.position + moveDirection * Time.fixedDeltaTime);

        isGrounded = Physics.CheckSphere(groundCheck.position, 0.1f, groundLayer);

        if (isGrounded && Input.GetButtonDown("Jump"))
        {
            rb.AddForce(Vector3.up * jumpSpeed, ForceMode.Impulse);
        }
    }
}
983 chars
37 lines
  1. Create enemy AI using C# scripts that control their movement, attack, and health. You can use the NavMeshAgent component to make the enemies move towards the player and the Animator component to play animation for attacks and death.
main.cs
using UnityEngine;
using UnityEngine.AI;

public class EnemyController : MonoBehaviour
{
    public Transform target;
    public float aggroRadius = 10.0f;
    public float attackRadius = 1.5f;
    public int maxHealth = 100;
    public int damage = 10;

    private NavMeshAgent agent;
    private Animator anim;
    private int currentHealth;

    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        anim = GetComponent<Animator>();
        currentHealth = maxHealth;
    }

    void FixedUpdate()
    {
        float distance = Vector3.Distance(transform.position, target.position);

        if (distance < aggroRadius)
        {
            agent.SetDestination(target.position);

            if (distance < attackRadius)
            {
                anim.SetTrigger("Attack");
                target.GetComponent<PlayerController>().TakeDamage(damage);
            }
            else
            {
                anim.SetBool("Walk", true);
            }
        }
        else
        {
            anim.SetBool("Walk", false);
        }
    }

    public void TakeDamage(int damage)
    {
        currentHealth -= damage;

        if (currentHealth <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        anim.SetTrigger("Die");

        GetComponent<Collider>().enabled = false;
        GetComponent<Rigidbody>().isKinematic = true;
        agent.enabled = false;

        Destroy(gameObject, 2.0f);
    }
}
1464 chars
68 lines
  1. Add user interface (UI) elements using Unity’s built-in UI system to display information such as the player’s health, ammo, and score.

With these basic steps and examples of C# code, you can start creating your own first person shooter game in Unity. There are also many tutorials and assets available in the Unity Asset Store and on online game development communities to help you with your game development journey.

gistlibby LogSnag