- Home /
Enemy shoot
i am not very experienced but i am trying my best .. so i have an enemy tank that looks at me and shoots but i can't seem to make it shoot one bullet at a time.. it just sprays it non-stop.. any help would be great
void Start () { }
void Update () {
if(Vector3.Distance (player.position,transform.position)<160){
Quaternion rotation = Quaternion.LookRotation (player.position-transform.position);
transform.rotation=Quaternion.Lerp(transform.rotation,rotation,1f);
InvokeRepeating ("Shoot",0.1f,0.2f);
}
}
void Shoot() {
GameObject KEBUMZ;
KEBUMZ= Instantiate (bullet, obj.position,obj.rotation ) as GameObject;
KEBUMZ.rigidbody.AddForce(obj.forward*12000f);
It's doing this because you invoke shoot in the update function. This way every tick when the tanks are close enought the function invokes Shoot.
You should call a function ins$$anonymous$$d of invokerepeating. For the function you should have a look at the machinegun script from the FPS Tutorial (If you don't know it google it).
Hope this helps.
Answer by aldonaletto · Jun 17, 2012 at 04:33 PM
InvokeRepeating is supposed to be called only once, and the function will be invoked at the desired rate. In your case it's better to have a nextShot timer:
float nextShot = 0; float shotInterval = 0.5f;
void Update () {
if(Vector3.Distance (player.position,transform.position)<160){
Quaternion rotation = Quaternion.LookRotation (player.position-transform.position);
transform.rotation=Quaternion.Lerp(transform.rotation,rotation,1f);
if (Time.time > nextShot){ // if is time to shoot...
Shoot(); // do it...
nextShot = Time.time + shotInterval; // and define the next shot time
}
}
}
void Shoot() {
GameObject KEBUMZ;
KEBUMZ= Instantiate (bullet, obj.position,obj.rotation ) as GameObject;
KEBUMZ.rigidbody.AddForce(obj.forward*12000f);
I cannot thank you enough, good man.. it has been 24 hours of trying to figure this out, my project is due in 5 days >.<.. again thank you.
Your answer
Follow this Question
Related Questions
Shoot. Just Shoot 5 Answers
How to stop enemies from shooting each other 1 Answer
Enemy fire does not propel in any direction? 1 Answer
Clones of enemies will not shoot 0 Answers