enemy code unity
/*if you want the code to work you need to do this steps
step 1 : you need to have on your enemy a collider.
step 2 : you will need to add the target (what the enemy goind to follow);
step 3 : you will need to add how match health he will have and how fast he will be;
step 4 : add how match damage he will do(if u want to have something that can die you will need to add to it a code the code can be found in this link https://youtu.be/BLfNP4Sc_iA)
step 5 : refrens what you want the enemy to do damage to
and the last thing is to add a death effect(if you want) happy to help!
*/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
Rigidbody rig;
public GameObject DealDamageTo;
public Transform target;
public GameObject deathEffect;
public int health = 100;
public float speed = 100f;
public int damage = 30;
void Start ()
{
rig = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
Vector3 pos=Vector3.MoveTowards(transform.position,target.position, speed * Time.fixedDeltaTime);
rig.MovePosition(pos);
transform.LookAt(target);
}
public void TakeDamage (int damage)
{
health -= damage;
if (health <= 0)
{
Die();
}
}
void Die ()
{
Instantiate(deathEffect, transform.position, Quaternion.identity);
Destroy(gameObject);
}
void OnTriggerEnter (Collider hitInfo)
{
CanDie DealDamageTo = hitInfo.GetComponent<CanDie>();
if (DealDamageTo != null )
{
DealDamageTo.TakeDamage(damage);
}
}
}