Search Unity

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

Discussion in 'Scripting' started by Whatee, Feb 24, 2010.

  1. Whatee

    Whatee

    Joined:
    Feb 22, 2010
    Posts:
    10
    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


    }
     
  2. Whatee

    Whatee

    Joined:
    Feb 22, 2010
    Posts:
    10
    My Bad


    this is my script!!
    The last one had a few errors


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

    function Update ()
    {
    //Look at a certain target
    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 * 1200);

    savedTime=seconds;
    }
    //Removed this peice of code, to make it more complex
    //transform.LookAt(LookAtTarget);
    }
     
  3. Eric Powell

    Eric Powell

    Joined:
    Apr 21, 2008
    Posts:
    10
    Add something like this to see if the target is "within range":

    Code (csharp):
    1. var dist = Vector3.Distance(LookAtTarget.position, transform.position);
    2. if (dist < 100) {
    3.     // do something if in range of 100 meters, etc.
    4. }
     
  4. Whatee

    Whatee

    Joined:
    Feb 22, 2010
    Posts:
    10
    I dont think that script alone will do anything? no?
     
  5. Essal

    Essal

    Joined:
    Feb 16, 2010
    Posts:
    107
    He means that you should add that into your script:)

    also, check out the FPS tutorials on the unity homepage..:)
    I believe FPS tutorial Part 2 or 3 has some guides on turrets - that only see shoot the player when they can see him within a certain range
     
  6. Whatee

    Whatee

    Joined:
    Feb 22, 2010
    Posts:
    10
    lol i figured he meant that,
    except i never said anything about FPS.

    and Adding this to my script didn't do anything besides add a (Distance Variable)
     
  7. Essal

    Essal

    Joined:
    Feb 16, 2010
    Posts:
    107
    I know you didn't say FPS,
    but the tutorial has exacly the kind of script you need - thats all I'm saying.
    Just check it out man, there is a functioning script that makes a turret rotate to the player and shoot when it sees the player , and he is within a certain range.