Search Unity

Native .net array conundrums!

Discussion in 'Scripting' started by Luke-Houlihan, Apr 22, 2008.

  1. Luke-Houlihan

    Luke-Houlihan

    Joined:
    Jun 26, 2007
    Posts:
    303
    I'm trying to get a heavily reworked version of SentryGun from the FPS tutorials to work, however I using it for autonomous towers that need to see many enemies and iterate through them and attack accordingly. This script worked when I was using GameObject.FindWithTag , but I wanted something faster and smother for when the number of enemies starts to increase. My idea was to implement a .net array because they are faster, and use that to track and do calculations on the enemies, but it seems flawed somehow because I cant get it to work at all.


    Code (csharp):
    1. private var enemy : GameObject;
    2. public static var EnemyArr : GameObject[];
    3. public static var n : int = 0;
    4.  
    5. function Start(){
    6.     EnemyArr = new GameObject[50];
    7. }
    8.  
    9. function Spawn(e : GameObject){
    10.     EnemyArr[n] = Instantiate (e, transform.position, transform.rotation );
    11.     n++;
    12.     for (var i : int = 0; i >=50; i++){
    13.         print(EnemyArr[i]);
    14.     }
    15.     if(n >=50){
    16.         n = 1;
    17.     }
    18. }
    19.  
    20.  
    This code is what I use to generate enemies at a spawn point. This is also supposed to insert the spawned gameobject into the array EnemyArr but im unsure whether that is even working.


    Code (csharp):
    1. var attackRange = 200.0;
    2. var target : GameObject;
    3. var enemyObj : GameObject;
    4. private var timer : int = 0;
    5. var tempArray : GameObject[];
    6.  
    7. function Awake(){
    8.     enemyObj = GameObject.Find("Sp1");
    9.     tempArray = new GameObject[50];
    10. }
    11.    
    12. function Update(){
    13.     timer++;
    14.     if (timer >= 20){
    15.         EnemyTracker();
    16.         timer = 0;
    17.     }
    18. }
    19.  
    20. function CanSeeTarget() : boolean{
    21.     if (Vector3.Distance(transform.position, target.transform.position) > attackRange){
    22.         return false;
    23.     }
    24.     var hit : RaycastHit;
    25.     if (Physics.Linecast (transform.position, target.transform.position, hit)){
    26.         return hit.transform == target;
    27.     }
    28.     return false;
    29.     target = null;
    30. }
    31.  
    32. function EnemyTracker(){
    33.     if ( target == null || Vector3.Distance(target.transform.position,transform.position) > attackRange){
    34.         for (var j : int = 0; j >= 50; j++ ){
    35.             enemy = enemyObj.GetComponent(EnemySpawner).EnemyArr[j];
    36.             //work out which item is the closest and shoot at it
    37.             var distance = Vector3.Distance(enemy.transform.position,transform.position);
    38.             if (distance < attackRange){;
    39.                 target = enemy;
    40.             }
    41.         }
    42.     }
    43.     if (target == null){
    44.             return;
    45.     }
    46.     if (CanSeeTarget ()){
    47.         return;
    48.     }
    49.    
    50.     //rotate towards target
    51.     var targetPoint = target.transform.position;
    52.     var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
    53.     transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 20);
    54.    
    55.     //if we are almost rotated towards target - fire
    56.     var forward = transform.TransformDirection(Vector3.forward);
    57.     var targetDir = target.transform.position - transform.position;
    58.     if (Vector3.Angle(forward, targetDir) * Mathf.Rad2Deg < 15.0){
    59.         BroadcastMessage("Fire");
    60.     }
    61.     //target = null;
    62. }
    63.  
    64.  
    Here is my Gun class, this seems like it should work for me, but doesnt do anything, the thing that makes this hard is that they both compile, but dont behave correctly at runtime.

    Thanks,
    Luke