Right now I have some scripts that allow the Enemy to die, and when he dies you get XP, and I tried adding some more Enemies, and implemented a targeting system, but you can be any distance. You can be 500 meters away from it, and you can sitll target/damage it. I am guessing I have to use Vector math, but not sure how to do this. I want to make it so if the distance between the player and the enemy is less than 2 (From any direction (Up, down, left, right, etc.) then he can’t target it, or damage it. If you need me to clarify more, let me know. Thanks in advance!
Here is the script with the targeting system and the DamageEnemy
using UnityEngine;
using System.Collections;
public class DamageEnemy : MonoBehaviour {
public GameObject Enemy;
public GameObject Enemy2;
public GameObject EnemyBoss;
public GameObject target;
public int number = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
CheckKey();
}
void OnGUI(){
DisplayTarget();
}
//-------------------------------------------------------------------------------------
void CheckKey(){
//EnemyStats ES = (EnemyStats)Enemy.GetComponent("EnemyStats");
//EnemyStats ES2 = (EnemyStats)Enemy2.GetComponent("EnemyStats");
if(Input.GetKeyDown(KeyCode.X)){
number = number + 1;
if(number > 3){
number = 1;
}
switch (number){
case 1:
target = Enemy;
break;
case 2:
target = Enemy2;
break;
case 3:
target = EnemyBoss;
break;
default:
target = null;
break;
}
}
//if(Input.GetKeyDown(KeyCode.C)){
// target = Enemy2;
//}
if(target == Enemy){
if(Input.GetKeyDown(KeyCode.Z)){
EnemyStats ES = (EnemyStats)Enemy.GetComponent("EnemyStats");
ES.EnemyHealth = ES.EnemyHealth - 25;
}
}
if(target == Enemy2){
if(Input.GetKeyDown(KeyCode.Z)){
EnemyStats ES = (EnemyStats)Enemy2.GetComponent("EnemyStats");
ES.EnemyHealth = ES.EnemyHealth - 25;
}
}
if(target == EnemyBoss){
if(Input.GetKeyDown(KeyCode.Z)){
EnemyStats ES = (EnemyStats)EnemyBoss.GetComponent("EnemyStats");
ES.EnemyHealth = ES.EnemyHealth - 25;
}
}
}
//-------------------------------------------------------------------------------------------
void DisplayTarget(){
if(target == Enemy)
{
EnemyStats ES = (EnemyStats)Enemy.GetComponent("EnemyStats");
GUI.Box(new Rect((Screen.width / 2 + 250) + 2, 36, 160, 25), ("Targeting: Enemy LVL:1" ));
GUI.Box(new Rect((Screen.width / 2 + 250) + 2, 62, 160, 25), ("Enemy Health: " + ES.EnemyHealth + "/" + ES.EnemyMaxHealth));
}
if(target == Enemy2)
{
EnemyStats ES = (EnemyStats)Enemy2.GetComponent("EnemyStats");
GUI.Box(new Rect((Screen.width / 2 + 250) + 2, 36, 160, 25), ("Targeting: Enemy LVL:2" ));
GUI.Box(new Rect((Screen.width / 2 + 250) + 2, 62, 160, 25), ("Enemy Health: " + ES.EnemyHealth + "/" + ES.EnemyMaxHealth));
}
if(target == EnemyBoss)
{
EnemyStats ES = (EnemyStats)EnemyBoss.GetComponent("EnemyStats");
GUI.Box(new Rect((Screen.width / 2 + 250) + 2, 36, 180, 25), ("Targeting: Enemy LVL: Boss" ));
GUI.Box(new Rect((Screen.width / 2 + 250) + 2, 62, 180, 25), ("Enemy Health: " + ES.EnemyHealth + "/" + ES.EnemyMaxHealth));
}
}
}
Here is the script where the Enemy dies and such (if that’s even needed?)
using UnityEngine;
using System.Collections;
public class EnemyStats : MonoBehaviour {
public int EnemyHealth = 100; //Change in inspector for each enemy this is put into.
public int EnemyMaxHealth = 100; //Change in inspector for each enemy this is put into.
public int DropXP = 11; //Change in inspector for each enemy this is put into.
public int DropGold = 8; //Change in inspector for each enemy this is put into.
public GameObject GiveStuff;
public GameObject GiveStuff2;
public bool IsAlive = true;
public bool IsTargeted = false;
// Use this for initialization
void Start () {
DropXP = DropXP / 2;
DropGold = DropGold / 2;
}
// Update is called once per frame
void Update () {
if(EnemyHealth < 0){
EnemyHealth = 0;
}
PlayerLevelDeclaration PT = (PlayerLevelDeclaration)GiveStuff.GetComponent("PlayerLevelDeclaration");
if(EnemyHealth <= 0 && IsAlive){
IsDead = true;
IsAlive = false;
EnemyHealth = 0;
if(IsDead = true && EnemyHealth == 0){
StartCoroutine(OnDeath());
IsDead = false;
}
}
}
bool IsDead = false;
IEnumerator OnDeath(){
AttributeDeclaration AT = (AttributeDeclaration)GiveStuff2.GetComponent("AttributeDeclaration");
PlayerLevelDeclaration PT = (PlayerLevelDeclaration)GiveStuff.GetComponent("PlayerLevelDeclaration");
if(EnemyHealth <= 0){
EnemyHealth = 0;
IsDead = true;
if(IsDead == true && EnemyHealth == 0){
AT.DroppedGold = DropGold;
PT.DroppedXP = DropXP;
yield return new WaitForSeconds(.00000000000000000000000000000000000000000000000000000000000000000000001f);
IsDead = false;
if(DropGold >= DropGold){
AT.DroppedGold = 0;
}
if(DropXP >= DropXP){
PT.DroppedXP = 0;
}
yield return new WaitForSeconds(2f);
collider.enabled = false;
renderer.enabled = false;
IsTargeted = false;
yield return new WaitForSeconds(10f);
IsDead = false;
IsAlive = true;
EnemyHealth = EnemyMaxHealth;
PT.DroppedXP = 0;
AT.DroppedGold = 0;
collider.enabled = true;
renderer.enabled = true;
}
}
yield return IsDead;
}
}