hi, I'm trying to make a 2d top down game where if I defeat an enemy, then the enemy fights for me.
I'm trying to destroy the original enemy object when health = 0 and will set is Dead bool to true, then the other enemy will spawn and attack enemies. but when i kill the enemy, the second enemy doesn't show.
here is my code for the second enemy script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class goblinzob : MonoBehaviour
{
public float speed;
private Transform target;
private Transform target2;
public Animator animator;
public Animator aNimator;
public Transform homePos;
public int maxHealth = 50;
public int currentHealth;
public HealthBar healthbar;
Vector2 movement;
public EnemyFollow ef;
// Start is called before the first frame update
void Start()
{
target = GameObject.FindGameObjectWithTag("Player").GetComponent();
target2 = GameObject.FindGameObjectWithTag("Enemy").GetComponent();
currentHealth = maxHealth;
healthbar.SetMaxHealth(maxHealth);
gameObject.SetActive(false);
}
// Update is called once per frame
void Update()
{
movement.x = target.position.x - transform.position.x;
animator.SetFloat("Horizontal", movement.x);
animator.SetFloat("Speed", movement.sqrMagnitude);
aNimator.SetFloat("Horizontal", movement.x);
aNimator.SetFloat("Speed", movement.sqrMagnitude);
if(Vector2.Distance(transform.position, target2.position) < 300 && ef.isDead == true)
{
transform.position = transform.position = Vector2.MoveTowards(transform.position, target2.position, speed * Time.deltaTime);
}
else if (ef.isDead == true)
{
transform.position = transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
if(Vector2.Distance(transform.position, target2.position) < 50f && ef.isDead == true)
{
aNimator.SetBool("IsAttacking", true);
}
else if (ef.isDead == true)
{
aNimator.SetBool("IsAttacking", false);
}
if(Vector2.Distance(transform.position, target.position) < 40f && ef.isDead == true)
{
animator.SetBool("HomePosition", true);
}
else if (ef.isDead == true)
{
animator.SetBool("HomePosition", false);
}
if(Vector2.Distance(transform.position, target2.position) < 40f && ef.isDead == true)
{
animator.SetBool("HomePosition", true);
}
else if (ef.isDead == true)
{
animator.SetBool("HomePosition", false);
}
if(ef.isDead == true)
{
gameObject.SetActive(true);
transform.position = target2.position;
}
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthbar.SetHealth(currentHealth);
}
}
↧