Hai i have an enemy, its working good, but when i create duplicates of the enemy, only the original can attack me. I can’t attack the duplicate, but if i attack a duplicate the original dies and duplicates stands there with default animation. My health variable is not static. here are my codes
Enemy_movement
public static Enemy_movement Instance;
Transform player;
NavMeshAgent nav;
// Use this for initialization
void Awake ()
{
Instance = this;
player = GameObject.FindGameObjectWithTag ("Player").transform;
nav = GetComponent<NavMeshAgent> ();
}
// Update is called once per frame
void Update ()
{
if (Enemy_Health.Instance.CurrentHealth > 0 && Player_Health.Instance.CurrentHealth > 0)
{
nav.SetDestination (player.position);
}
else
{
nav.enabled = false;
}
}
Enemy_Attack
public static Enemy_Attack Instance;
public float TimeBetweenAttack = 0.5f;
public float distance;
public int AttackDamage = 10;
public float[] AnimLength = new float[3] {0.458f, 0.500f, 0.500f};
public GameObject targetLookAt;
GameObject player;
Animator anim;
bool inAttackRange;
//Enemy_Health enemyHealth;
float timer = 0f;
int attackCount=0;
// Use this for initialization
void Awake ()
{
Instance = this;
player = GameObject.FindGameObjectWithTag ("Player");
anim = GetComponent<Animator> ();
targetLookAt = GameObject.Find ("targetLookAt") as GameObject;
}
void Update ()
{
timer -= Time.deltaTime;
distance = Vector3.Distance(targetLookAt.transform.position, gameObject.transform.position);
Debug.Log ("Distance=" + distance);
if (distance <= 2.5f)
{
inAttackRange = true;
}
else if (distance < 10f)
{
inAttackRange = false;
anim.Play("attack_walk");
anim.SetBool ("InRange", true);
attackCount = 0;
timer = 0f;
}
else
{
anim.SetBool ("InRange", false);
}
if (Player_Health.Instance.CurrentHealth <= 0) {
anim.SetTrigger ("PlayerDead");
} else
{
if (inAttackRange&& Enemy_Health.Instance.CurrentHealth > 0)
{
Attack ();
}
}
}
void Attack()
{
if (timer <= 0f)
{
if (attackCount == 0)
{
timer = AnimLength [0];
timer += 0.3f;
anim.Play("attack_1");
attackCount++;
}
else if(attackCount == 1)
{
timer = AnimLength [1];
timer += 0.3f;
anim.Play("attack_2");
attackCount++;
}
else if(attackCount == 2)
{
timer = AnimLength [2];
timer += 0.3f;
anim.Play("attack_3");
attackCount++;
}
else if(attackCount > 2)
{
attackCount = 0;
}
}
}
public void DamagePlayer() //animation event function
{
if (Enemy_Weapon_Collision.Instance.colliding)
{
if (Player_Health.Instance.CurrentHealth > 0)
Player_Health.Instance.TakeDamage (AttackDamage);
}
}
Enemy_Health
public static Enemy_Health Instance;
public int StartingHealth = 100;
public int CurrentHealth;
public int ScoreValue = 10;
private GameObject ragdoll;
// Use this for initialization
void Awake ()
{
Instance = this;
CurrentHealth = StartingHealth;
}
// Update is called once per frame
void Update ()
{
}
public void TakeDamage (int amount)
{
CurrentHealth -= amount;
if (CurrentHealth <= 0)
{
Death();
}
}
void Death()
{
if (ragdoll == null)
{
ragdoll = GameObject.Instantiate(Resources.Load("Ragdoll_samurai"),
transform.position,
transform.rotation) as GameObject;
}
var characterRoot = transform.FindChild("metarig");
var ragdollRoot = ragdoll.transform.FindChild("metarig");
//Match the ragdoll's skeleton to character's skelecton
MatchChildrensTransforms (characterRoot, ragdollRoot);
//destroy the character
Destroy(gameObject);
Collision_check.Instance.InRange = false;
//Tell the camera to look at the ragdoll
TP_Camera.Instance.TargetLookAt = ragdoll.transform.FindChild("metarig/Root");
}
void MatchChildrensTransforms(Transform source, Transform target)
{
//Traverse through the skeleton hierarchy to matching joint rotation
if (source.childCount > 0)
{
foreach (Transform sourceTransform in source.transform)
{
Transform targetTransform = target.Find(sourceTransform.name);
if (targetTransform != null)
{
MatchChildrensTransforms(sourceTransform,targetTransform);
targetTransform.localPosition = sourceTransform.localPosition;
//targetTransform.localRotation = sourceTransform.localRotation;
}
}
}
}
Player_Health
public static Player_Health Instance;
public int StartingHealth = 100;
public int CurrentHealth;
public Slider HealthSlider;
public Image DamageImage;
public float FlashSpeed = 5f;
public Color FlashColour = new Color (1f, 0f, 0f, 0.1f);
Animator anim;
bool isDead;
bool damaged;
// Use this for initialization
void Awake ()
{
Instance = this;
anim = GetComponent<Animator> ();
CurrentHealth = StartingHealth;
}
// Update is called once per frame
void Update ()
{
if (damaged)
DamageImage.color = FlashColour;
else
DamageImage.color = Color.Lerp (DamageImage.color, Color.clear, FlashSpeed * Time.deltaTime);
damaged = false;
}
public void TakeDamage (int amount)
{
damaged = true;
CurrentHealth -= amount;
HealthSlider.value = CurrentHealth;
if (CurrentHealth <= 0 && !isDead)
{
Death();
}
}
void Death()
{
isDead = true;
anim.SetTrigger ("Dead");
TP_Animator.Instance.Die ();
}
please help, this is my project and am stuck here, thank you.
,