Search Unity

using a turret to attack multiple enemies

Discussion in 'Scripting' started by xjenkinx800, Sep 5, 2011.

  1. xjenkinx800

    xjenkinx800

    Joined:
    Sep 4, 2011
    Posts:
    2
    If anybody can help me it would be greatly appreciated. I am trying to set up the turret from the FPS tutorial to kill multiple enemies and it is not working. Originally it was set to attack the fps with the (player) tag. But I added a new tag (enemies), which seem to work only for one gameobject that is with that tag. I would like it to kill all gameoject with that tag. any info or suggestion will be helpful

    and I did change the FindWithTag(Player) to ("Enemies")
    function Start () { if (target == null GameObject.FindWithTag("Enemies")) target = GameObject.FindWithTag("Enemies").transform; still did not work
     
  2. 95KillerZ95

    95KillerZ95

    Joined:
    May 27, 2011
    Posts:
    253
    If I've understood you want hit all enemies with that tag, so you have to create a trigger for the turret area, and make a foreach as this:

    Code (csharp):
    1.  
    2.  
    3. void OnTriggerEnter(Collider obj)
    4. {
    5.      if(obj.gameobject.tag == "Enemy")
    6.            obj.GetComponent("EnemyScript").Hit(10);
    7. }
    8.  
    9.  
     
  3. xjenkinx800

    xjenkinx800

    Joined:
    Sep 4, 2011
    Posts:
    2



    do I need to add this to my script that i am already using are create another script and attach it to the object with the collider (trigger) here the script that i am using thank u for your reply (i am new to programming).

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

    function Start () {
    if (target == null GameObject.FindWithTag("Enemies"))
    target = GameObject.FindWithTag("Enemies").transform;
    }

    function Update () {
    if (target == null)
    return;

    if (!CanSeeTarget ())
    return;

    // Rotate towards target
    var targetPoint = target.position;
    var targetRotation = Quaternion.LookRotation (targetPoint - transform.position,

    Vector3.up);
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation,

    Time.deltaTime * 2.0);

    // If we are almost rotated towards target - fire one clip of ammo
    var forward = transform.TransformDirection(Vector3.forward);
    var targetDir = target.position - transform.position;
    if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
    SendMessage("Fire");
    }

    function CanSeeTarget () : boolean
    {
    if (Vector3.Distance(transform.position, target.position) > attackRange)
    return false;

    var hit : RaycastHit;
    if (Physics.Linecast (transform.position, target.position, hit))
    return hit.transform == target;

    return false;
    }
     
  4. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Your problem is you only set a target on start... So it targets the first available object with your tag. Once it's dead... No more target. You need to have it search for a new target everytime it doesn't have a target if you want it to attack more then 1 thing.... If that makes sense. Pretty much if target = null... Redo what's in your start function, or come up with a different target detection system.

    Hope that helps