Answers for "unity 2d shooting cooldown"

C#
2

top down shooting in unity 2D

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

public class Shooting : MonoBehaviour
{

    public Transform firePoint;
    public GameObject bulletPrefab;

    public float bulletForce = 20f;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            Shoot();
        }
    }  

    void Shoot()
    {
        GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
        Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
        rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
    }

}
Posted by: Guest on July-27-2020
3

unity 2d shooting cooldown

// Very light and easy to use cooldown tool for unity:
// https://github.com/JosepeDev/CooldownAPI

void Shoot()
{
  // some shooting behaviour
  ...
  // cooldown activation
  shootingCooldown.Activate();
}

if (//if player is pressing the shoot button)
{
  // prevent the player from shooting when cooldown active
  if (shootingCooldown.IsActive == false)
  {
    Shoot();
  }
}
Posted by: Guest on August-19-2021

C# Answers by Framework

Browse Popular Code Answers by Language