Search Unity

Gun Detection Problem.

Discussion in 'Scripting' started by Hansuke, Jun 13, 2007.

  1. Hansuke

    Hansuke

    Joined:
    Apr 17, 2007
    Posts:
    100
    Code (csharp):
    1. var attackRange = 30.0;
    2. var shootAngleDistance = 10.0;
    3. var target : Transform;
    4. var target2 : Transform;
    5. function Start(){
    6.    transform.LookAt(target2);  
    7. }        
    8.    function Update(){
    9.          if (target == null  GameObject.FindWithTag("Finish")) {
    10.                 target = GameObject.FindWithTag("Finish").transform;
    11.          }
    12.    if (!CanSeeTarget ())
    13.       return;
    14.    
    15.    if (target == null  GameObject.FindWithTag("Finish")){
    16.       target = GameObject.FindWithTag("Finish").transform;
    17.    }
    18.      
    19.    // Rotate towards target  
    20.    var targetPoint = target.position;
    21.    var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
    22.    transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
    23.  
    24.    // If we are almost rotated towards target - fire one clip of ammo
    25.    var forward = transform.TransformDirection(Vector3.forward);
    26.    var targetDir = target.position - transform.position;
    27.    if (Vector3.Angle(forward, targetDir) < shootAngleDistance)
    28.       SendMessage("Fire");
    29. }
    30.  
    31. function CanSeeTarget () : boolean
    32. {
    33.    if (Vector3.Distance(transform.position, target.position) > attackRange)
    34. return false;  
    35.    var hit : RaycastHit;
    36.    if (Physics.Linecast (transform.position, target.position, hit))
    37.       return hit.transform == target;
    38.  
    39.    return false;
    40. }
    the script above is the script i use for my sentrygun . It will detect the monster itself and shoot'em.

    The problem is here,when i have just a spawn point, it will detect the monster and shoot it, no problem. But when i duplicate another spawn point(means having 2 spawn point now), the sentry will only detect the 2nd spawn point's monster first, after killing it then only goes back to the first spawn point's monster.

    So i try doing this, i made a spawn point, duplicate it, and place the 2nd spawn point far far away, so result was the sentrygun will wait the 2nd spawn point's monster to arrive and kill it and ignore the monster from the first spawn point which came first, and when they killed the one far far away, then it only turn back and shoot the first spawn point's one.

    any idea? :?