Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Helicopter attack each hood?

Discussion in 'Scripting' started by TSC, Apr 14, 2014.

  1. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Hey guys Rio here, I'm still in the build of The Drop City, and i have this helicopter script where i need it to attack the closest target on the map when approaching after being triggered.....

    Its identifying each hood already but i can't seem to get it to identify each hood then select the closest one then engage it... and after its done with that target go to the next one....any help anyone?


    Code (csharp):
    1. var targets : GameObject[];
    2. var ClosestTarget : Transform;
    3. var rotationSpeed = 5;
    4. var myTransform : Transform;
    5. var canMove : boolean = true;
    6. var inCombat : boolean = false;
    7. var missleLaunch : Transform;
    8. var AttckactiveHoods : int = 0;
    9. var HoodHealthScript : HoodHealth;
    10. var _distanceFromEnemy : float = 0;
    11.  
    12.  
    13. function Awake(){
    14.     HoodCheck1();
    15.     myTransform = transform;
    16.     HoodHealthScript = GameObject.FindObjectOfType(typeof(HoodHealth)) as HoodHealth;
    17.     targets = GameObject.FindObjectOfType(typeof(Hood)) as GameObject[];
    18. }
    19.  
    20.  
    21.  
    22. function Start(){
    23.     //target = AttckactiveHoods.transform;  
    24. }
    25.  
    26.  
    27.  
    28. function Update () {
    29.     var moveSpeed = Random.Range(30,80);
    30.  
    31.   //check to make sure not being attack. if attacked then inCombat = true. applies to both enemy players and other minions.
    32.  
    33.  
    34.   //if no enemy no longer in distance continue towards tower. inCombat == false
    35.  
    36.  
    37.  //head to the next enemy if no enemy is in range or been attacked
    38.    
    39.  
    40.     if(canMove == true  inCombat == false  HoodHealthScript.tower1destroyed == false) {
    41.          myTransform.rotation = Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(ClosestTarget.position - myTransform.position), rotationSpeed*Time.deltaTime);
    42.          myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;  
    43.  }
    44.  
    45.  
    46.  //if incombat == true then target / attack nearest enemy
    47.  
    48. }
    49. //trigger while stay. Attack tower. If enemies come close then attack enemies instead so switch to inCombat
    50. //towers not dead then keep attacking tower
    51. function HoodCheck1() {
    52.         var hoods : Hood[] = FindObjectsOfType(Hood) as Hood[];
    53.         for (var hood : Hood in hoods) {
    54.             AttckactiveHoods++;
    55.         }
    56.         _distanceFromEnemy = Vector3.Distance(transform.position, ClosestTarget.position);
    57.    
    58.    
    59.         if(_distanceFromEnemy <= 2.0){
    60.            
    61.            
    62.         }
    63.         else
    64.         if(_distanceFromEnemy <= 4.0){
    65.             transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(ClosestTarget.position - transform.position), Time.deltaTime * 4);
    66.             transform.Translate(0, 0, .11f);
    67.        
    68.         }else{
    69.             //transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(_Player.position - transform.position), Time.deltaTime * 4);
    70.             transform.Translate(0, 0, .1f);
    71.    
    72.            
    73.         }
    74.    }   
    75. function OnTriggerEnter (col : Collider) {
    76.     if(col.gameObject.tag == "HeliTrig"){
    77.         Debug.Log("entered City");
    78.     if(HoodHealthScript.tower1destroyed == false) {
    79.         canMove = false;
    80.         Instantiate(missleLaunch,this.transform.position,Quaternion.identity);
    81.             }
    82.         }
    83. }
     
  2. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Bump
     
  3. djfunkey

    djfunkey

    Joined:
    Jul 16, 2012
    Posts:
    201
    Code (csharp):
    1.  
    2. public Transform FindClosestTransformToPoint(GameObject[] hoods, Vector3 point) {
    3.         float distance = Mathf.Infinity;
    4.         Transform result = null;
    5.         for(int i = 0; i < hoods.Length; ++i) {
    6.             if(!hoods[i].enabled) continue;
    7.             float thisDist = Vector3.Distance(point, hoods[i].transform.position);
    8.             if(thisDist < distance) {
    9.                 distance = thisDist;
    10.                 result = hoods[i];
    11.                 Gizmos.DrawLine(point, hoods[i].transform.position);//See the closest point in editor
    12.             }
    13.         }
    14.         return result;
    15.     }
    16.  
     
  4. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Thanks alot for responding, how can i write it in java, the public transform .... I have been tryin for the last hr ...
     
  5. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Bump
     
  6. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Please don't bump your thread. If you are not getting the help you need it's because your question is not clear. Rather than being lazy and say "bump", or not-quite-so-lazy and say "I have been tryin for the last hr" why not post the JS code you have tried, and have problems with.
     
  7. TSC

    TSC

    Joined:
    Oct 15, 2012
    Posts:
    271
    Didn't know that, appologies