- Home /
how to add firerate to enemies shooting script
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);
}
}
Your answer
Follow this Question
Related Questions
Enemy fire rate? help! 2 Answers
FPS shooting 1 Answer
[Closed] Enemy Shooting isn't working correctly 0 Answers
Shooting and health Help 0 Answers