duplicating enemies not working correctly
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.
,
Answer by MathieuBarbier · Sep 15, 2015 at 01:31 PM
You are using a singleton pattern for your health managers. This pattern only allows one instance of a class to be used at any given time. You should remove all those "instances" variables and use another architecture if you want to use multiple players / ennemies...
So how do I do it?? can you give me an example?? do I have to redo all my codes?? this is my project and gotta submit by 28 this month, I still have some animations to do too. Please help. and thank you for your reply..... :)
You have to remove all these instances vars and be able to target your gameobject specifically. You have multiple ways to do that. You can use GameObject.Find for the quick and ugly way. Youcan also use gameobject references...
hai I did as you said and it worked, but created a new problem, now enemies are not following the character anymore, it has something to do with the player_health script. its not being accessed by gameobject.getcomponent(); because its values are not changing and its functions are not being called. one note though all my other script of the player are made using Instance, except player_health script. is that why ?? or is it possible to give an object both of these script?? my other player scripts are not accessing any value from this class. thank you for the quick reply
Your answer
Follow this Question
Related Questions
If one prefab enemy dies, the other enemy prefabs malfunction 0 Answers
enemy death physics 0 Answers
Enemy damage script is not working 0 Answers