Search Unity

enemy teams used FindGameObjectWithTag("teamx");

Discussion in 'Scripting' started by ganixx, Apr 26, 2015.

  1. ganixx

    ganixx

    Joined:
    Feb 2, 2015
    Posts:
    29
    hello peopple i´m noob in unity and i have a problem to make a combat team sistem.
    i try to make all units of team1 (for example) can attack to a random unit of team 2 .

    in my code all units of the team attack the same unit and i have no idea how to make the units to attack ramdomly.

    Code (csharp):
    1.  
    2.  
    3. var Estado : int = 0;
    4.  
    5. var Target: GameObject; //El objetivo
    6.  
    7. private var RotInic : Quaternion;
    8. var RotSpeed : float;
    9.  
    10. var VelMov : float;
    11. var gravity : float = 9.8;
    12.  
    13. private var DistanciaCont : float;
    14. var DistEnem : float = 0.5;
    15.  
    16. private var contador = 0.0;
    17.  
    18. //Animaciones
    19.  
    20. var IdleAnim : AnimationClip;
    21. var RunAnim : AnimationClip;
    22. var GuardAnim : AnimationClip;
    23. var AttackAnim : AnimationClip;
    24.  
    25. var MuerteAnim : AnimationClip;
    26.  
    27. function Start ()
    28. {
    29.    //wrapMode puede ser: Once, Loop, PingPong, Default o ClampForever
    30.    animation[IdleAnim.name].speed = 1;
    31.    animation[IdleAnim.name].wrapMode = WrapMode.Loop;
    32.  
    33.    animation[RunAnim.name].speed = 1;
    34.    animation[RunAnim.name].wrapMode = WrapMode.Loop;
    35.  
    36.    animation[GuardAnim.name].speed = 1;
    37.    animation[GuardAnim.name].wrapMode = WrapMode.Loop;
    38.  
    39.    animation[AttackAnim.name].speed = 1;
    40.    animation[AttackAnim.name].wrapMode = WrapMode.Once;
    41.  
    42.    animation[MuerteAnim.name].speed = 1;
    43.    animation[MuerteAnim.name].wrapMode = WrapMode.ClampForever;
    44.  
    45.    RotInic = transform.rotation;
    46.  
    47.    animation.Play(IdleAnim.name);
    48.    
    49.    if (Target == null)
    50.    {
    51.       Target = GameObject.FindGameObjectWithTag("Player");
    52.    }
    53.  
    54. }
    55.  
    56. function Update ()
    57. {
    58.  
    59.    var controller : CharacterController = GetComponent(CharacterController);
    60.  
    61.    if (Estado == 0)
    62.    {
    63.    
    64.     //Target = GameObject.FindGameObjectWithTag("Player");
    65.       //Acción  
    66.       //Cambio de Estado y Activar siguiente animación.
    67.       //Se cambia mediante un disparador externo.
    68.    }
    69.    if (Estado == 1) //Perseguir
    70.    { Target = GameObject.FindGameObjectWithTag("Player");
    71.       //Acción
    72.       //Girar y avanzar.
    73.       transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(Target.transform.position - transform.position), RotSpeed * Time.deltaTime);
    74.       transform.rotation.x = RotInic.x;
    75.       transform.rotation.z = RotInic.z;          
    76.       //Cambio de Estado y Activar siguiente animación.
    77.      
    78.       controller.Move(transform.forward * VelMov * Time.deltaTime);
    79.       //Aplicar gravedad
    80.       controller.Move(transform.up * -gravity * Time.deltaTime);
    81.      
    82.       //Cambio de Estado y Activar siguiente animación.
    83.  
    84.       DistanciaCont = Vector3.Distance(Target.transform.position, transform.position);
    85.       if (DistanciaCont <= DistEnem)
    86.       {
    87.          Estado = 2;
    88.          animation.CrossFade(GuardAnim.name);
    89.       }
    90.    }
    91.    if (Estado == 2) //Guardia
    92.    {Target = GameObject.FindGameObjectWithTag("Player");
    93.       //Acción
    94.       //Cambio de Estado y Activar siguiente animación.
    95.       DistanciaCont = Vector3.Distance(Target.transform.position, transform.position);
    96.       if (DistanciaCont > DistEnem)
    97.       {
    98.          Estado = 1; //Pasa al estado de perseguir.
    99.          animation.CrossFade(RunAnim.name);
    100.       }else{
    101.          Estado = 3;//Pasa al estado de Atacar.
    102.          contador = Time.time + (animation[AttackAnim.name].clip.length * 1.2);
    103.          //El tiempo actual + (el tiempo de la animación y un pelín más)
    104.          animation.Play(AttackAnim.name);        
    105.       }
    106.    }
    107.    if (Estado == 3) // Atacar
    108.    {
    109.       //Acción
    110.    
    111.       //Cambio de Estado y Activar siguiente animación.
    112.       if (Time.time > contador)
    113.       {
    114.          Estado = 2;
    115.          animation.CrossFade(GuardAnim.name, 2.0f);
    116.       }
    117.    }
    118.    
    119.    
    120.    
    121. }
    122.  
    123. function Muerte ()
    124. {
    125.    Estado =9;
    126.    animation.Play (MuerteAnim.name);
    127. }
    128.  
    129. function DoActivateTrigger ()
    130. {
    131.    Estado = 1;
    132.    animation.CrossFade(RunAnim.name);
    133. }
    134.  
    135. function DoDesactivateTrigger ()
    136. {
    137.    Estado = 0; //cambiar a al estado de Inactivo
    138.    animation.Play(IdleAnim.name);
    139. }
    140.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  3. ganixx

    ganixx

    Joined:
    Feb 2, 2015
    Posts:
    29
    mmm i read the documentation but i dont understand how to make what you say
     
  4. ganixx

    ganixx

    Joined:
    Feb 2, 2015
    Posts:
    29
    if i put this in update fuction
    var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Enemy");

    its work fine or make an error?

    or i need make another function and return a random numer,and that number is the number of the enemy unit?
     
  5. hamsterbytedev

    hamsterbytedev

    Joined:
    Dec 9, 2014
    Posts:
    353
    That will return an array of GameObjects with the tag "Enemy". If that's what you're trying to do then you got it :)
     
  6. ganixx

    ganixx

    Joined:
    Feb 2, 2015
    Posts:
    29
  7. ganixx

    ganixx

    Joined:
    Feb 2, 2015
    Posts:
    29
    tks ,
    its work