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



      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.


Take a look at this


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.