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);
        }
        

    }

}

Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source