- Home /
Other
Distance before shooting issue.
I can't get the AI to shoot at the correct range which is referenced from another script called RifleAI.
Well it works almost perfect if I type in a number but not if I put in the float range.
Let me explain in Code:
// This is where the range is referenced from aka the rifle script.
private Rifle weaponRifleScript;
void Update(){
if(canShoot == true){
Shooting();
}else{
canShoot = false;
}
}
void Shooting(){
targetDistance = Vector3.Distance(player.position, target.position);
// this is where it gets the range from the rifle script.
float range = weaponRifleScript.range;
if(targetDistance < range){
weaponRifleScript.InvokingRepeating("Shoot", 0.5f, 20);
}
}
So yeah, its simply not working, either it shoots from what ever range or does not shoot at all.
Heeeelp....
Answer by getyour411 · Aug 24, 2013 at 02:51 AM
Take a look at this
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.InvokeRepeating.html
One issue seems to be your methodname in InvokeRepeating is "Shoot" but you named it "Shooting". Also, you calculate targetDirection but then on 25 refer to targetDistance.
Its invoking the shoot from another script, IE the weapon.
Shooting() is from this specific script.
Shoot() pretty much just instantiates the bullets with delay. From the other script.
Nope that was just typo.
Its got something to do with the range for sure.
Oh wait I think I fixed it...
It was because I was a noob and I didn't check the ranges properly.
100 was what the range float was, which was further than I thought it was in game.
I'm not too sure though lol, its late and life is a blur.
Thanks though for feedback and link.