Question by
Salim Saadi · Aug 24, 2015 at 12:39 AM ·
instantiateenemyshooting
how to make enemy shoot one only time !!
the enemy keep shooting my player ,,i have no idea how to stop him. Any help please !! PS: this is my script
void shooting(){
float pos = player.transform.localPosition.x;
differance = (transform.position.x) - pos;
bool isClose=false;
if (Mathf.Abs((transform.position.x) - pos) < 25) {
moveSpeed=5;
}
if (Mathf.Abs((transform.position.x) - pos) < 12) {
moveSpeed=0;
if(differance<0){
transform.localScale=new Vector3(2.5f ,3f ,1f);
}
else
if(differance>0){
transform.localScale=new Vector3(-2.5f ,3f ,1f);
}
if(differance<20){
Instantiate(enemiesfire, firePointEnem.position , firePointEnem.rotation);
}
}
Comment
Best Answer
Answer by pRoFlT · Aug 24, 2015 at 03:18 AM
you could add this to your code.
//you need some variables first
public float fireRate=1.0f; // for 1 second. adjust as needed 0.3, 8.0, etc...
public float fire_dt;
// and in "void update()" add
fire_dt+=Time.deltaTime;
// in your fire routine.
if(fire_dt>fireRate) // this makes the enmy only be able to shoot in intervals of your fireRate
{
Instantiate(enemiesfire, firePointEnem.position , firePointEnem.rotation);
fire_dt=0;
}
Also. i see you have <20. test it in the editor to see how much 20 apart really is? seems to be a big number to me. I'm not 100% sure how unity handles x dimensions but for a game i'm working on right now 60 is the entire screen size. so 20 would be a third for me. but i guess it's all how you setup the camera distance etc...