Hello,
I am making a game for a school project and I have an animated enemy set up. Basically what I want to do now is duplicate them and put them at certain points on the map. Problem is, when I ctrl+D the enemy in the editor they all get shot when I shoot óne of the enemies and they also ALL die when I kill one.
Please bare with me since I am a novice at unity and coding in general.
My enemy consists of three scripts:
This is the Raycast script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HandGunDamage : MonoBehaviour {
int DamageAmount = 5;
float TargetDistance;
float AllowedRange = 30;
public static bool Is_Hit = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
RaycastHit Shot;
if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), out Shot))
{
TargetDistance = Shot.distance;
if (TargetDistance < AllowedRange)
{
Shot.transform.SendMessage("DeductPoints", DamageAmount, SendMessageOptions.DontRequireReceiver);
Is_Hit = true;
}
}
}
}
}
This is the script that handles the animation and the dying of the enemies:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class SpiderScript : MonoBehaviour
{
public GameObject Player, Enemy001, Enemy002;
public Animator Anim;
public NavMeshAgent NavMeshAgent;
private float Distance_To_Player;
private float m_AnimationTime;
// Use this for initialization
void Start()
{
Anim = GetComponent<Animator>();
NavMeshAgent = GetComponent<NavMeshAgent>();
}
// Update is called once per frame
void Update()
{
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z);
Distance_To_Player = Vector3.Distance(Player.transform.position, transform.position);
if (!HandGunDamage.Is_Hit)
{
if (Distance_To_Player > 3 && Distance_To_Player < 20 && EnemyScript.EnemyHealth > 0)
{
Anim.SetBool("b_IsChasing", true);
NavMeshAgent.SetDestination(Player.transform.position);
}
else if (Distance_To_Player > 20)
{
NavMeshAgent.SetDestination(transform.position);
Anim.SetBool("b_IsChasing", false);
}
if (EnemyScript.EnemyHealth <= 0)
{
NavMeshAgent.Stop();
Anim.SetTrigger("t_Die");
}
}
else if (HandGunDamage.Is_Hit)
{
NavMeshAgent.SetDestination(transform.position);
Anim.SetTrigger("t_IsHit");
if (!Anim.GetCurrentAnimatorStateInfo(0).IsName("t_IsHit"))
{
HandGunDamage.Is_Hit = false;
}
}
}
}
And last but not least, the scripts that handles the deduction of healthpoints of the enemy:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Threading;
public class EnemyScript : MonoBehaviour {
public static int EnemyHealth = 10;
// Use this for initialization
void Start () {
}
void DeductPoints(int DamageAmount)
{
EnemyHealth -= DamageAmount;
}
// Update is called once per frame
void Update () {
}
}
What is the best way of making unique duplicates of this enemy?
I apologize for the long text.
Any help would be much appreciated!