'Shooting cooldown for enemy in unity2d
I'm doing a top-down shooter in unity, I'm trying to make the enemy shoot when it sees the player. So far, this is my code:
public class EnemyShooting : MonoBehaviour
{
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 20f;
public FieldOfView _fieldofview;
void Start()
{
_fieldofview = FindObjectOfType<FieldOfView>();
}
// Update is called once per frame
void Update()
{
if(_fieldofview.canSeePlayer)
{
Shoot();
}
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb2d = bullet.GetComponent<Rigidbody2D>();
rb2d.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
}
However, when the player is detected it just spams the bullets due it doesn't has a cool down timer. I've tried with a Coroutine and the Invoke method but it doesn't works. Any ideas?
Solution 1:[1]
You don't need to use a SELECT in such function.
So try this
CREATE FUNCTION CalcPrice (
@OrgPrice decimal(10,2),
@PrdPrice decimal(10,2)
)
RETURNS decimal(10,2)
AS
BEGIN
DECLARE @Result decimal(10,2);
SET @Result = @OrgPrice - @PrdPrice;
RETURN @Result;
END;
SELECT *
, dbo.CalcPrice(PriceOrg, PrdPrice) AS Price
FROM Product
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | LukStorms |
