Example 1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
public float speed; // speed of enemy movement
public float detectionRange; // distance at which enemy will begin chasing player
public float attackRange; // distance at which enemy will attack player
public int attackDamage; // amount of damage enemy will deal to player when attacking
public GameObject player; // reference to player game object
private Rigidbody2D rb; // reference to enemy's rigidbody
private bool isChasing; // whether or not the enemy is currently chasing the player
private bool isAttacking; // whether or not the enemy is currently attacking the player
private Vector2 moveDirection; // direction in which the enemy is currently movingprivate Rigidbody2D rb; // reference to enemy's rigidbody
bool isChasing; // whether or not the enemy is currently chasing the player
private bool isAttacking; // whether or not the enemy is currently attacking the player
private Vector2 moveDirection; // direction in which the enemy is currently moving
private void Start()
{
rb = GetComponent<Rigidbody2D>();
isChasing = false;
isAttacking = false;
}
// Update is called once per frame
void Update()
{
// check distance to player and determine if enemy should chase or attack
float distanceToPlayer = Vector2.Distance(transform.position, player.transform.position);
if (distanceToPlayer <= attackRange)
{
isChasing = false;
isAttacking = true;
}
else if (distanceToPlayer <= detectionRange)
{
isChasing = true;
isAttacking = false;
}
else
{
isChasing = false;
isAttacking = false;
}
if (isChasing)
{
// chase the player
moveDirection = player.transform.position - transform.position;
moveDirection.Normalize();
rb.velocity = moveDirection * speed;
}
else if (isAttacking)
{
// attack the player
player.GetComponent<PlayerController>().TakeDamage(attackDamage);
}
else
{
// idle
rb.velocity = Vector2.zero;
}
}
}
Example 2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyAI : MonoBehaviour
{
// movement variables
public float moveSpeed;
public bool moveRight;
// platform variables
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
// attacking variables
public float attackRange;
public float attackCooldown;
private float attackTimer;
public int damage;
// player target
private Transform target;
private void Start()
{
// find player target
target = GameObject.FindGameObjectWithTag("Player").transform;
}
/// <summary>
/// Update is called every frame, if the MonoBehaviour is enabled.
/// </summary>
private void Update()
{
// check if enemy is on the ground
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
// if player is within attack range and attack timer is reset
if (Vector2.Distance(transform.position, target.position) < attackRange && attackTimer <= 0)
{
// attack the player
Attack();
}
else
{
// move towards player
if (target.position.x > transform.position.x && moveRight)
{
// face right and move right
transform.localScale = new Vector3(-1f, 1f, 1f);
GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
else if (target.position.x < transform.position.x && !moveRight)
{
// face left and move left
transform.localScale = new Vector3(1f, 1f, 1f);
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
}
// decrease attack timer
if (attackTimer > 0)
{
attackTimer -= Time.deltaTime;
}
}
void Attack()
{
// damage player and reset attack timer
target.GetComponent<PlayerHealth>().TakeDamage(damage);
attackTimer = attackCooldown;
}
}
Create an enemy game object and add a collider to it so that it can detect collisions with the player and other objects in the scene.
Add a script to the enemy game object to control its behavior. In this script, you can define the enemy's movement patterns, such as patrolling a certain area or chasing the player.
Use the Unity API to detect when the enemy collides with the player or other objects in the scene. For example, you can use the OnCollisionEnter2D
method to detect when the enemy collides with the player, and then have the enemy attack or chase the player.
Use Unity's built-in physics system to handle enemy movement. You can use the Rigidbody2D
component to apply forces to the enemy game object and make it move.
Use Unity's built-in animation system to animate the enemy's movements and actions. For example, you can use the Animator
component to play different animations based on the enemy's state (such as idle, walking, or attacking).
Test and refine the enemy AI to ensure that it behaves as desired. You can use the Unity editor to adjust the enemy's movement patterns and other behavior-related parameters, and then playtest the game to see how the enemy behaves.
If you have any question let me know in the comment.
Post a Comment
image video quote pre code