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

Attack within range not working.

Discussion in 'Scripting' started by andrew159, Feb 15, 2016.

  1. andrew159

    andrew159

    Joined:
    Mar 30, 2015
    Posts:
    18
    I need help and guidance from you guys.
    I try adding " Shoot if in range" to my enemy attack script and it doesn't seem to be working.
    Even though i move my Player to a very far distance, the enemy still able to shoot me.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class EnemyShoot : MonoBehaviour {
    5.  
    6.     public Transform player;
    7.  
    8.     public float range = 10f;
    9.     public float bulletImpulse = 1.0f;
    10.     public float mistakeRadius;
    11.     private bool onRange= false;
    12.  
    13.  
    14.  
    15.     public Rigidbody projectile;
    16.  
    17.     void Start()
    18.     {
    19.         float rand = Random.Range (1.0f, 2.0f);
    20.         //                     1st timer for shotting
    21.         InvokeRepeating("Shoot", 5, rand);
    22.     }
    23.        
    24.  
    25.     void Shoot()
    26.     {
    27.        
    28.         int randomPick = Mathf.Abs (Random.Range (1, 3));
    29.         if (onRange)
    30.             {
    31.            
    32.  
    33.  
    34.             if (randomPick == 1)
    35.             {
    36.  
    37.                 Rigidbody bullet = (Rigidbody)Instantiate(projectile, transform.position + transform.forward, transform.rotation);
    38.                 //bullet.AddForce (transform.forward * bulletImpulse, ForceMode.Impulse);
    39.                 var desiredDirection = (player.position - transform.position) + Random.onUnitSphere;
    40.                 bullet.AddForce(desiredDirection * bulletImpulse, ForceMode.Impulse);
    41.                 Destroy (bullet.gameObject, 2);
    42.  
    43.             }
    44.  
    45.             if (randomPick == 2)
    46.             {
    47.  
    48.                 Rigidbody bullet = (Rigidbody)Instantiate(projectile, transform.position + transform.forward, transform.rotation);
    49.                 var desiredDirection = (player.position - transform.position) + (Random.onUnitSphere * mistakeRadius);
    50.                 bullet.AddForce(desiredDirection * bulletImpulse, ForceMode.Impulse);
    51.                 Destroy (bullet.gameObject, 2);
    52.  
    53.             }
    54.    
    55.             }
    56.  
    57.  
    58.     }
    59.  
    60.     void Update()
    61.     {
    62.  
    63.         onRange = Vector3.Distance(transform.position, player.position) < range;
    64.         if (onRange)
    65.         {
    66.             transform.LookAt (player);
    67.  
    68.         }
    69.            
    70.     }
    71.  
    72.  
    73. }
     
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Wha?

    in his update, he states... onRange =.....

    In his shoot he says... if(onRange).....

    On line 67, type this in...

    Code (csharp):
    1.  
    2. Debug.Log(Vector3.Distance(transform.position, player.position));
    3.  
     
  4. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    Sry missed a line there, was rather late where i am when I took a look at this.
     
  5. andrew159

    andrew159

    Joined:
    Mar 30, 2015
    Posts:
    18
    The distance is updating but the enemy still kept shooting even though it's out of the give range.
     
  6. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    what exactly was the range though
     
  7. andrew159

    andrew159

    Joined:
    Mar 30, 2015
    Posts:
    18
    Guess what guys...I just realize that when i change the range value in the script, it doesn't update in the prefab. LOL!
    I just manual change it and it work =.= Seriously?
    But thank you so much in helping me. And also thanks for the link.
     
  8. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    LOL, and that would be why, when developing, I always use Private variables.