- Home /
how to make an enemy fire a bullet once every second
i have a very basic enemy AI script to make the enemy follow me and when within range shoot at me but it continuously fires bullets extremely fast. i want it to fire bullets much slower than it does. any way i can instantiate the bullet every second instead of constantly?
my AI code is:
var bullet : Transform;
var bulletSpawn : Transform;
function Update ()
{
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist){
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist)
{
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
}
}
}
There are several ways to make this happen. In order to get an accurate and well targeted answer, we need to see your code. Please edit your question and add your shooting code.
Answer by TargonStudios · Dec 12, 2013 at 01:32 AM
You could add in a simple variable. For example, you could add in a variable "CanShoot", once the enemy has shot, set it to false. Then call on a reload function, after a few seconds, make it true again.(Although, without your script, its harder to answer. You should always post your script along with your question) Example:
var CanShoot = true;
function Update(){
if(CanShoot == true){
//InRange scripts
//Shot scripts
CanShoot = false;
Reload();
}
}
function Reload(){
yield WaitForSeconds(1);
CanShoot = true;
}
Hope this helps!
It's better to use InvokeRepeating i think, it's simple and easier in general.
I prefer this way because I can use it in both player Guns, and NPC guns.(I like keeping things the same)
If it answered your question, why not mark it as correct? ;D
is there a way I could maybe remove the person looking at you?
Answer by ThiagoTejo · Dec 12, 2013 at 01:33 AM
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html
Take a look at this. Its very simple, just put your firing code in a function, and use InvokeRepeating with 1 second time. The exemple in the docs is basically what you're looking for.
Also, don't use Invoke Repeating in the Update function. use it in Start or other function you have.
using in the Update function will have it starting every frame, and that's not you're looking for.
Answer by robertbu · Dec 12, 2013 at 01:42 AM
The easiest solution is to use a timestamp:
var bullet : Transform;
var bulletSpawn : Transform;
var timeBetweenShots = 1.0;
private var timestamp = 0.0;
function Update () {
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= MinDist) {
transform.position += transform.forward*MoveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= MaxDist && (Time.time > timestamp)) {
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
timestamp = Time.time + timestamp;
}
}
}
Small mistake it must be
var bullet : Transform;
var bulletSpawn : Transform;
var timeBetweenShots = 1.0;
private var timestamp = 0.0;
function Update () {
transform.LookAt(Player);
if(Vector3.Distance(transform.position,Player.position) >= $$anonymous$$inDist) {
transform.position += transform.forward*$$anonymous$$oveSpeed*Time.deltaTime;
if(Vector3.Distance(transform.position,Player.position) <= $$anonymous$$axDist && (Time.time > timestamp)) {
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
timestamp = Time.time + timeBetweenShots;
}
}
}
Answer by Solber · Aug 01, 2020 at 03:56 PM
Here is my working C# code :
float distanceToPlayer = Vector3.Distance(transform.position, other.transform.position);
if (hitDist < distanceToPlayer && moveToPlayer)
{
transform.position = Vector2.MoveTowards(
transform.position,
other.transform.position,
MoveSpeed * Time.deltaTime);
}
else if (hitDist >= distanceToPlayer)
{
moveToPlayer = false;
if (canFire)
{
canFire = false;
timestamp = Time.time + 1.0f;
hitPlayer();
}
else if (Time.time > timestamp)
{
canFire = true;
}
}
Your answer
Follow this Question
Related Questions
Instantiating a pattern in front of the player? 1 Answer
Instantiated bullets hitting GameObjects? 4 Answers
How to check who shot whom 3 Answers
Shot Delay between button press. c# 2 Answers
Turret bullet rotation problem 1 Answer