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);
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);