well, Iam working on my enemy AI. But i have a problem. If the enemy attack, the player only lose damage when the enemy hits the player. But also when i walk on his feet, and it doesn’t matter if he missed it wil his sword. I have no idea how to fix this.`using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Enemy_AI_Troll : MonoBehaviour {
//The nav mesh agent
public NavMeshAgent agent;
//The target
public GameObject target;
//Enemies
public List<GameObject> enemies;
//List with allies
public List<GameObject> Allies;
//Closest enemy
public GameObject Closest;
//sword
public GameObject sword;
//the fist variable
public Transform Fist;
//The number of closest enemy
public int Closest_Number;
//The Attackrange varaible
public int Attack_Range;
//The shoot range
public int Shoot_Range;
//the hit range variable
public int Hit_Range;
//The number of weapon it has equiped
public int weapon_num;
//damage
public int damage;
//health
public int health;
//The distance between all enemies and soldier
public List<float> Distance;
//The closest distance
public float Closest_Distance;
//List with all the tags
public string Tags;
//this is a boolean that wil be true if it is the first time that the game is launched
public bool First_Time;
//When the soldier is alive
public bool alive;
//this boolean is used to say if the distance has been calculated
public bool Distance_Calculated;
//this boolean is used to say if the closed enemy has been calculated
public bool Closest_Calculated;
//can swing
public static bool Can_Swing;
public RaycastHit hit;
void Awake(){
health = 100;
damage = 20;
}
// Use this for initialization
void Start () {
//calls the function to set all the variable's
Set_Variable();
alive = true;
agent = this.GetComponent<NavMeshAgent>();
//it says automaticly that the game has been launched for the first time, because there will other loaded if it is not
First_Time = true;
//Cal the function for adding all the tags
Add_Tag();
//set enemy ally
Set_Enemy_Ally();
//alive
Alive();
//can swing
Can_Swing = true;
}
void Set_Variable(){
//The hitrange variable will be set to 2 meter
Hit_Range = 3;
//the attack range variable will be set to 10 meters
Attack_Range = 50;
//closest number wil be set to -1
Closest_Number = -1;
}
// Update is called once per frame
void Update () {
//When the Soldier is alive
if(alive == true){
//Then it will cal the function Alive
Alive();
}
if(transform.forward. magnitude > 2){
GetComponent<Animation>().Play("Run");
}
}
//This function will add al the tag's
void Add_Tag(){
}
//This function will set all the enemies and allies
void Set_Enemy_Ally(){
//when it is the first time that the game has been launched
if(First_Time == true){
//Then it will set the tag's
enemies.AddRange(GameObject.FindGameObjectsWithTag("Enemy"));
Distance.AddRange(new float[enemies.Count]);
}
}
//This will call all the functions when a soldier is alive
void Alive(){
//It will call a function for the attack part, the soldier will only attack when the enemy is close
Distance_Calculate();
//the scan function
Scan_Attack();
//WHen the closest has been calculated
if(Closest_Calculated == true){
//Then it will cal the function for scanning if the enemy is close enough to attack
Scan_Distance_For_Attack();
}
if(Distance[Closest_Number] < Hit_Range){
Swing ();
}
}
//The function for calculating the distance
void Distance_Calculate(){
//this is a loop, and i used it to calculate each enemies distance
for(int i = 0; i < enemies.Count; i++){
//This wil calculate the distance
Distance _= Vector3.Distance(transform.position, enemies*.transform.position);*_
* //When all the distances has been calculated*
* if(i == enemies.Count){*
* //the boolean distance_calculated will be true*
* Distance_Calculated = true;
_ }_
_ }*_
* }*
* //This function is for scanning if the enemie is close enough to attack*
* void Scan_Attack(){
_ //It wil start with a loop*_
* for(int i = 0; i < enemies.Count; i++){*
* //Here it will start comparing*
if(Closest_Number == -1 || Closest_Distance > Distance*){
_ Closest = enemies;_
Closest_Number = i;
Closest_Distance = Distance;
Closest_Calculated = true;
_ }
}
}*_
* //This function is for comparing the distance to the attack range*
* void Scan_Distance_For_Attack(){
_ //If the distance is closer then the attack range*
* if(Closest_Distance < Attack_Range && Closest_Distance > 1){*
* //then it will set the as target, the closest enemy*
* target = Closest;
agent.SetDestination(target.transform.position);*_
* }*
* //when not*
* else{*
* //when the enemy is not in the right range*
* agent.SetDestination(transform.position);*
* }*
* //When the enemy is in hit range*
* if(Closest_Distance < Hit_Range){
_ //then it wil call the function for swinging*_
* }*
* }*
* void Swing(){*
* //it wil only look at the target*
* transform.LookAt(Closest.transform.position);*
* if(Can_Swing == true){
_ //then it will play the attack animation*
* GetComponent().Play(“Attack_01”);*
* //when the weapon collides or almost collides*
* if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit)){
float distance = hit.distance;
if(distance < 2){
hit.transform.SendMessage(“ApplyDamage”, damage);
}
}_
Can_Swing = false;
_ StartCoroutine (Timer());
}
}
IEnumerator Timer(){
yield return new WaitForSeconds(2.0f);_
Can_Swing = true;
_ }*_
* }*
`
I hope someone could help me