First Step
So, this is a simple AI for follow and attack the player in a 2d platformer game in unity, it is not the optimal solution but it is the easiest and simplest.
So first we have to add a rigidbody2d for the enemies and don't forget to add a collider so it wont fall down and an animator to trigger the animations.
Second Step
Let's create a base enemy class so our enemies can inherit from it because usually we will have multiple types of enemies in the game so every enemy can have his own life points , damage etc...
first let's name our base class enemy, create a c sharp script and add the following code
public class Enemies : MonoBehaviour
{
//base class so it can be inherited from other scripts
int moveSpeed;
int attackDamage;
int lifePoints;
float attackRadius;
//movement
float followRadius;
public void setMoveSpeed(int speed)
{
moveSpeed = speed;
}
public void setAttackDamage(int attdmg)
{
attackDamage = attdmg;
}
public void setLifePoints(int lp)
{
lifePoints = lp;
}
public int getMoveSpeed()
{
return moveSpeed;
}
public int getAttackDamage()
{
return attackDamage;
}
public int getLifePoints()
{
return lifePoints;
}
//movement toward a player
public void setFollowRadius(float r)
{
followRadius = r;
}
//attack radius
public void setAttackRadius(float r)
{
attackRadius = r;
}
//if player in radius move toward him
public bool checkFollowRadius(float playerPosition, float enemyPosition)
{
if(Mathf.Abs(playerPosition -enemyPosition) < followRadius)
{
//player in range
return true;
}
else
{
return false;
}
}
//if player in radius attack him
public bool checkAttackRadius(float playerPosition, float enemyPosition)
{
if (Mathf.Abs(playerPosition - enemyPosition) < attackRadius)
{
//in range for attack
return true;
}
else
{
return false;
}
}
}
Third Step
Now we will create the actual movement, let's create a script called EnemyMove and let's add this code
public class EnemyMove : Enemies
{
//variables
public int _moveSpeed;
public int _attackDamage;
public int _lifePoints;
public float _attackRadius;
//movement
public float _followRadius;
//end
[SerializeField] Transform playerTransform;
[SerializeField]Animator enemyAnim;
SpriteRenderer enemySR;
void Start()
{
//get the player transform
playerTransform = FindObjectOfType<Player>().GetComponent<Transform>();
//enemy animation and sprite renderer
enemyAnim = gameObject.GetComponent<Animator>();
enemySR = GetComponent<SpriteRenderer>();
//set the variables
setMoveSpeed(_moveSpeed);
setAttackDamage(_attackDamage);
setLifePoints(_lifePoints);
setAttackRadius(_attackRadius);
setFollowRadius(_followRadius);
}
// Update is called once per frame
void Update()
{
if (checkFollowRadius(playerTransform.position.x,transform.position.x))
{
//if player in front of the enemies
if (playerTransform.position.x < transform.position.x)
{
if (checkAttackRadius(playerTransform.position.x, transform.position.x))
{
//for attack animation
enemyAnim.SetBool("AttackA", true);
}
else
{
this.transform.position += new Vector3(-getMoveSpeed() * Time.deltaTime, 0f, 0f);
//for attack animation
enemyAnim.SetBool("AttackA", false);
//walk
enemyAnim.SetBool("Walking", true);
enemySR.flipX = true;
}
}
//if player is behind enemies
else if(playerTransform.position.x > transform.position.x)
{
if (checkAttackRadius(playerTransform.position.x, transform.position.x))
{
//for attack animation
enemyAnim.SetBool("AttackA", true);
}
else
{
this.transform.position += new Vector3(getMoveSpeed() * Time.deltaTime, 0f, 0f);
//for attack animation
enemyAnim.SetBool("AttackA", false);
//walk
enemyAnim.SetBool("Walking", true);
enemySR.flipX = false;
}
}
}
else
{
enemyAnim.SetBool("Walking", false);
}
}
}
Hope you like it!
Top comments (9)
I'm getting "Assets\Enemy\EnemyMove.cs(23,36): error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)" I only copy-pasted the third step and added
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
I think you just have to rename your player to "Player"
Yes sorry for late reply you should rename your player as metioned by Lonely.dk
after i done the 3rd step there is a error appeared that says Assets\script\EnemyMove.cs(24,44): error CS0246: The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)
even tho my player is named "Player"
Please help I did everything and then it tells me "Player" could not be found even though my controllable character is called "Player".
Do your player character have a script called player? My line goes playerTransform = FindObjectOfType().transform; Since the script on my player is called PlayerController.
That worked for me thank you.
Hello I have very much enjoyed this tutorial. I was wondering how to animate it however as it ask for an animator and I tried to use an animator controller and a animation and it didn't help. Any assistance would be very much appreciated.
i have a whats is Player transform and Enemy Animation...cuz i am a bigginer