Script so that, my enemies only see my player at a certain range.

Hi everyone,

I've been following the Tornado Twins tutorials on youtube and i just reached a snag.

In my scene i have turrets, whose head portion rotates along the base in order to aim at my player character. At the moment the turrets will aim and fire at my player character no matter where he is in game.

What i would like is for the Turrets to be offline or not working until the player character or TARGET has come within a certain range.

Thx.

This is my Script so Far

-------------------------------------------------------------------------------------------

var LookAtTarget:Transform; var damp = 6.0; var bulletPreFab:Transform; var savedTime=0;

var attackRange = 30.0; var shootAngleDistance = 10.0; var target : GameObject;

function Update() { }

if(LookAtTarget)
{
    var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
//Dampin/Delay on speed at which it follows the target
    transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

var seconds : int = Time.time; var oddeven = (seconds % 2);

if(oddeven) {

        Shoot(seconds); 
    }
}</p>
//Turret Shoot function

function Shoot(seconds) { if(seconds!=savedTime) { var bullet = Instantiate(bulletPreFab, transform.Find("SpawnPoint").transform.position, Quaternion.identity); //Bullet speed bullet.gameObject.tag = "enemyProjectile"; bullet.rigidbody.AddForce(transform.forward * 800);

savedTime=seconds; } //Removed this peice of code, to make it more complex //transform.LookAt(LookAtTarget); //if an enemy as further than maxDistance from you, it cannot see you

}

You would just want to do a distance check to the target... Something like Vector3.Distance(LookAtTarget.position, transform.position) should do the trick. This will return a float of the distance between the 2 points; compare that distance to your desired distance.

If your player has a rigidbody and is tagged "Player" (if you're using the default FPS Walker prefab, it already has these things), you can do the following:

Add a sphere collider to your turrets (make sure you "Add" and don't "Replace" any other colliders you have on the turret).

Make the sphere collider a trigger.

Make the radius equal to your desired detection distance.

Add the following to your turret script:

function OnTriggerEnter (other : Collider) {
    if (other.CompareTag("Player")) {
        // lookAtTarget corresponds to the LookAtTarget variable you defined.
        // In Unityscript, the preferred style is to start variables with a lowercase.
        lookAtTarget = true;
    }
}

function OnTriggerExit (other : Collider) {
    if (other.CompareTag("Player")) {
        lookAtTarget = false;
    }
}

Letting Unity's collision system handle distances is preferable to checking the distance every frame (see Unity's Optimization Docs).

I finished there tutorials too, here is my script with a few added bits, it looks at you but dost fire until your in range, you can mess around with these settings if you like, if you have not solved you problem then copy this, if your solved then OK then

//Movment and Target

var LookAtTarget:Transform; var damp = 1.7; var ememy : Transform;

//Shooting var bullitPrefab:Transform; var savedTime=0; var curShoot = true;

function Update () { if(LookAtTarget) {

         if ( Vector3.Distance( ememy.position, transform.position ) < 18 ) //'18' is the distance the turret starts to look at you </p>
         var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);

         transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);

         if(curShoot)
         {     
                var seconds : int = Time.time;
                var oddeven = (seconds % 2);

                if(oddeven)
                {     
                       Shoot(seconds);
                }
         } 
  }
}     

function Shoot(seconds)
{    
      if ( Vector3.Distance( ememy.position, transform.position ) < 15 ) //'15' is the distance the turret starts to shoot at you
      {
           if(seconds!=savedTime)
           {     
                  var bullit = Instantiate(bullitPrefab ,transform.Find("SpawnPoint").transform.position , 
                                                  Quaternion.identity);
                  bullit.gameObject.tag = "ememyProjectile";
                  bullit.rigidbody.AddForce(transform.forward * 1600);  
                  bullit.rigidbody.AddForce(transform.up * 45);
                  savedTime=seconds;
           }      
       }
}

Hope this helps

hehe, Thx for the help.

Just not exactly sure where i would put that in?