Question by
SHUTTER_CRY_GAMES · Mar 26, 2017 at 07:04 PM ·
2d-platformerenemy aiontriggerenter2d
How do i make a simple 2D AI enemy?
I have a 2D game where, if you meet an enemy, the enemy will: check if you're in the trigger; calculate if you're left or right of the AI; move towards you.
My issue is: The AI will only follow my player if it has JUST entered the box collider. I want it to change constantly, so if you jump over the enemy, the enemy will readjust to move towards you (X axis only, which is intended). RIGHT NOW: The AI will only readjust if you leave and reenter the box collider.
How do i fix this, I've tried using 'while' but it crashes the engine as soon as i enter the collider.
Below is the main chunk of the code not including the variables that I've already stated.
void Update()
{
enemyx = transform.position.x;
playerx = player.transform.position.x;
}
void OnTriggerEnter2D(Collider2D colliderInfo)
{
if(colliderInfo.name == "player")
{
enemyx = transform.position.x;
playerx = player.transform.position.x;
if(enemyx > playerx)
{
enemy.AddForce(enemyspeed*Time.deltaTime);
}
if(enemyx < playerx)
{
enemy.AddForce(-enemyspeed*Time.deltaTime);
}
}
Comment
Your answer
