x


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

//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

}

more ▼

asked Feb 24 '10 at 06:40 PM

Whatee gravatar image

Whatee
-4 2 3 4

Could you fix the code segments of your question? It's difficult to read what's going on with the formatting the way it is.

Feb 25 '10 at 02:10 PM burnumd
(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

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.

more ▼

answered Feb 24 '10 at 06:47 PM

Shawn gravatar image

Shawn
323 5 6 13

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Feb 24 '10 at 08:34 PM

burnumd gravatar image

burnumd
3.3k 22 34 71

Where should i add this script?

Feb 24 '10 at 09:07 PM Whatee

Place it anywhwere outside your Update function inside the turret script. These are both separate functions you can implement in any script and if a collider is attached to the same object, these functions are automatically called when a trigger event occurs (see the chart at the bottom of this page for the conditions: http://unity3d.com/support/documentation/Components/class-BoxCollider.html ).

Feb 25 '10 at 02:09 PM burnumd

I was having trouble with the same script and used your script but get this: Assets/scripts/TurretControl.js(29,32): BCE0022: Cannot convert 'boolean' to 'UnityEngine.Transform'.

Oct 29 '12 at 05:15 PM bdover1284
(comments are locked)
10|3000 characters needed characters left

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

         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

more ▼

answered Nov 15 '10 at 10:13 PM

Grimlock257 gravatar image

Grimlock257
23 3 3 7

(comments are locked)
10|3000 characters needed characters left

hehe, Thx for the help.

Just not exactly sure where i would put that in?

more ▼

answered Feb 24 '10 at 07:08 PM

Whatee gravatar image

Whatee
-4 2 3 4

When replying to an answer, please use the "add comment" link rather than posting a new "answer" to the question.

Feb 24 '10 at 07:42 PM runevision ♦♦

You can put it wherever works best for your script. Maybe where you select the LookAtTarget just do a check before assigning that value.

Feb 24 '10 at 08:30 PM Shawn
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5056
x652
x106
x44

asked: Feb 24 '10 at 06:40 PM

Seen: 3070 times

Last Updated: Oct 29 '12 at 05:15 PM