TopDownShooter Tutorial in UNITY. PART 11 – Weapon System.

Hi, new episode of Top Down Shooter tutorial is available under this link:

https://youtu.be/QuMOKVyr0n4

CODE:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class WeaponSystem : MonoBehaviour
{
    public Weapon[] weapons;
    public Weapon EquippedWeapon;
    public TextMeshProUGUI ammoTXT;
    // Start is called before the first frame update
    void Start()
    {
        EquippedWeapon = weapons[0];
        UpdateAmmoTXT();
    }

    // Update is called once per frame
    void Update()
    {
        SwitchWeapons();
    }

    public void EquipGun(int _gunToEquip)
    {
        EquippedWeapon = weapons[_gunToEquip - 1];
        UpdateAmmoTXT();
    }

    public void SwitchWeapons()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            EquipGun(1);

        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            EquipGun(2);
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            EquipGun(3);
        }
    }
    public void UpdateAmmoTXT()
    {
        ammoTXT.text = EquippedWeapon.ammo.ToString();
    }

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Weapon : MonoBehaviour
{
    public Bullet bullet;
    public Transform[] firePoints;
    public ParticleSystem flash;

    public float damage = 1;
    public float shotsPerSecond;
    public float nextShotTime;
    public int ammo;
    // Start is called before the first frame update

    public void Shoot()
    {
        if (nextShotTime <= Time.time && ammo>0)
        {
            foreach(Transform firePoint in firePoints)
            {
                Bullet _bullet = Instantiate(bullet, firePoint.position, firePoint.rotation);
                _bullet.damage = damage;
            }
            
            flash.Play();

            nextShotTime = Time.time + (1 / shotsPerSecond);
            ammo--;
        }
        
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class PlayerController : MonoBehaviour
{
    
    public WeaponSystem weapons;
    public Animator aimCursorAnim;

    public float moveSpeed = 2.0f;
    Vector2 moveDir; // moveDir(x,y) w =1, s =-1
    Rigidbody2D rb2d;

    Vector2 mousePos;
    // Start is called before the first frame update
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
        moveDir.x = Input.GetAxisRaw("Horizontal"); //x =1 or x=-1
        moveDir.y = Input.GetAxisRaw("Vertical");

        mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

        if (Input.GetButton("Fire1"))
        {
            Shoot();
        }
    }

    private void FixedUpdate()
    {
        Move();
        RotateToMousePos();
    }

    public void Move()
    {
        Vector2 _moveDir = rb2d.position + moveDir * moveSpeed * Time.fixedDeltaTime;
        _moveDir.x = Mathf.Clamp(_moveDir.x, -6.5f, 10);
        _moveDir.y = Mathf.Clamp(_moveDir.y, -5.0f, 11);
        rb2d.MovePosition(_moveDir);
    }

    public void RotateToMousePos()
    {
        Vector2 dirToMousePos = mousePos - rb2d.position;
        transform.up = new Vector2(dirToMousePos.x, dirToMousePos.y);

    }

    public void Shoot()
    {
        if (weapons.EquippedWeapon.nextShotTime <= Time.time && weapons.EquippedWeapon.ammo>0)
        {
            Camera.main.GetComponent<Animator>().SetTrigger("Shake");
            aimCursorAnim.SetTrigger("Shoot");
            
        }
        

        weapons.EquippedWeapon.Shoot();
        weapons.UpdateAmmoTXT();
    }
    

}

Leave a Reply

Your email address will not be published. Required fields are marked *