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

trying to see if distance from object is met then call function

Discussion in 'Scripting' started by usanoman, Feb 19, 2015.

  1. usanoman

    usanoman

    Joined:
    Feb 2, 2015
    Posts:
    37
    im trying to make it so that when the player only gets so and so close to the enemy and the mousebutton is down(Fire1) then it will subtract from health (By the way i admit i copied this from the enemy health script from the survival shooter and tweaked it just a tad...) this is what i have so far :
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class EnemyHealth : MonoBehaviour
    5. {
    6.     public float Distance1;
    7.     public float maxdistance = 1.5;
    8.         public int startingHealth = 100;             // The amount of health the enemy starts the game with.
    9.         public int currentHealth;
    10.     bool isDead;
    11.     void Awake ()
    12.     {
    13.      
    14.      
    15.      
    16.         // Setting the current health when the enemy first spawns.
    17.         currentHealth = startingHealth;
    18.     }
    19.  
    20.     public void TakeDamage (int amount, Vector3 hitPoint)
    21.     {
    22.  
    23.             // If the enemy is dead...
    24.             if (isDead)
    25.             // ... no need to take damage so exit the function.
    26.                 return;
    27.      
    28.  
    29.  
    30.         if (Input.GetButtonDown ("Fire1")) {
    31.             if  //Check to see if user is close enough to the enemy//{
    32.  
    33.             }
    34.             // Reduce the current health by the amount of damage sustained.
    35.             currentHealth -= amount;
    36.  
    37.         }
    38.      
    39.      
    40.      
    41.      
    42.             // If the current health is less than or equal to zero...
    43.             if (currentHealth <= 0) {
    44.                 // ... the enemy is dead.
    45.                 Death ();
    46.             }
    47.  
    48.         }
    49.  
    50.     void Death ()
    51.     {
    52.         // The enemy is dead.
    53.         isDead = true;
    54.      
    55.      
    56.      
    57.         // Tell the animator that the enemy is dead.
    58.         Destroy (gameObject);
    59.      
    60.      
    61.      
    62.     }
    63. }
    64.  
    65.  
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    So this script is attached to the ENEMY? The class is called EnemyHealth but I'm guessing that the "Fire" action should be associated with the PLAYER script. Might need some cleaning up.

    Anyway, to do the distance check you just need to use Vector3.Distance to compare the transform.position of your ENEMY and the transform.position of your PLAYER:

    http://docs.unity3d.com/ScriptReference/Vector3.Distance.html
     
  3. usanoman

    usanoman

    Joined:
    Feb 2, 2015
    Posts:
    37
    i get what your saying now if i am correct attach this to the enemy script then remove the mousebutton down and put it under the player then call the enemy health script from the player script if thats what you mean, im new to programming(sorta) so im not sure if this is what you mean or not
     
  4. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,066
    That's exactly what I mean :)