Newer
Older
HardPoint-Project-Abertay-University-Unity3D / Assets / Content / Developers / Miguel / MiguelToys / MachineGun.cs
@Rackday Rackday on 18 Aug 902 bytes Project Added
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MachineGun : MonoBehaviour
{
    [SerializeField]
    private float coolDown;

    [SerializeField]
    private GameObject bullet;

    [SerializeField]
    private float offset;

    [SerializeField]
    private float speed;

    private float timer;

    private void Start()
    {
        timer = coolDown;
    }
    public void Attack()
    {
        if (timer >= 0)
            timer -= Time.deltaTime;
        else
        {
            //Debug.Log("Shoot");
            Vector3 dir = Vector3.Normalize(Input.mousePosition - new Vector3(Screen.width / 2, Screen.height / 2, 0));
            GameObject temp = Instantiate(bullet, transform.position + (dir * offset), Quaternion.identity);
            temp.GetComponent<Rigidbody>().velocity = dir * speed;
            timer = coolDown;
        }
    }
}