i have a script where the enemy will shoot a projectile at the player if the player gets too close, but there is no way to make the enemy shoot faster or slower; it just shoots in slow intervals. how can i add a variable in which i can change the firerate to make the enemy shoot faster or slower? thanks for any help
here is my script:
using UnityEngine;
using System.Collections;
public class enemyshoot : MonoBehaviour
{
public Transform player;
public float range = 50.0f;
public float bulletSpeed = 10.0f;
private bool onRange = false;
public Rigidbody projectile;
public GameObject bulletSpawn;
void Start()
{
float rand = Random.Range(1.0f, 2.0f);
InvokeRepeating("Shoot", 1, rand);
}
void Shoot()
{
if (onRange)
{
Rigidbody bullet = (Rigidbody)Instantiate(projectile, bulletSpawn.transform.position + transform.forward, transform.rotation);
bullet.AddForce(transform.forward * bulletSpeed, ForceMode.Impulse);
Destroy(bullet.gameObject, 2);
}
}
void Update()
{
onRange = Vector3.Distance(transform.position, player.position) < range;
if (onRange)
transform.LookAt(player);
}
}
↧