# 2D-Enemy AI problem (2problems)

I just tried to make a simple AI for a 2d-game. The Ai just should follow the player and deal damage when it collides.
The 1st problem is, that the enemy slows down, when he gets negative coordinates (maybe its because -*- equals +) but i have no clue how to fix this on an easy way, without making thousands of if-statements.

``````using UnityEngine;
using System.Collections;

public class EnemyAI : MonoBehaviour {

public GameObject player;
public float movementSpeed = 2f;
// Use this for initialization
void Start () {
player = GameObject.Find ("Player");
}

// Update is called once per frame
void FixedUpdate () {

if(player.transform.position.x < transform.position.x){
rigidbody2D.velocity = new Vector2 ((transform.position.x*movementSpeed)*-1,rigidbody2D.velocity.y);

}
if(player.transform.position.x > transform.position.x){
rigidbody2D.velocity = new Vector2 (transform.position.x*movementSpeed,rigidbody2D.velocity.y);

}
if(player.transform.position.y < transform.position.y){
rigidbody2D.velocity = new Vector2 ((transform.position.x*movementSpeed)*-1,rigidbody2D.velocity.x);

}
if(player.transform.position.x > transform.position.x){
rigidbody2D.velocity = new Vector2 (transform.position.x*movementSpeed,rigidbody2D.velocity.x);

}

}
}
``````

The 2nd problem is that i dont know how to access the health variable, that is inside of the PlayerHealthscript, from the enemyAI script.

using UnityEngine;
using System.Collections;

public class PlayerHealthScript : MonoBehaviour {

``````public int maxHealth = 300;
public int currentHealth = 100;
public float healthbarLength;

// Use this for initialization
void Start () {
healthbarLength = Screen.width/4;
}

// Update is called once per frame
void Update () {

}

void OnGUI(){
GUI.Box (new Rect(10,10,healthbarLength,20),currentHealth + "/" + maxHealth);
}

if(currentHealth < 0)
currentHealth = 0;

if(currentHealth > maxHealth)
currentHealth = maxHealth;
if(maxHealth <1)
maxHealth =1;

healthbarLength = (Screen.width / 4) * (currentHealth/(float)maxHealth);
}
``````

}

I would be happy if someone at least can explain me how to solve at least 1 of the 2 problems. It is appreciated to give me tips how to improve my code to make it more efficient.
Maybe the way I set this up is completely wrong.

//Sorry for my bad english

For the first one, Id recommend using LookAt to lookat the player and then simply make them move in their transform.foward direction(this is their locally foward which should be facing the player.

This is way easier than all that up/down/left/right checking.

For the 2nd problem. In the enemy script use a OnCollisionEnter(look these up!) This will trigger when the object it is attached to collides with something(they both need colliders!) It passes in a collision variable and with that you can check if it hit your player and then you can use a GetComponent().AdjustCurrentHealth([some number]).

Thanks, your tips kind of worked. The only problem was, that my enemy rotatet 270 degrees on the x axis but i fixed it by adding

``````transform.rotation = Quaternion.Euler (lockRot,lockRot,lockRot);
``````

lockRot is a variable set to 0.

It isnt the fine way but it works.
I also changed the vector 3 to vector 2 because i dont like to mix up 2d and 3d.

And the enemy now moves that way:

``````        targetX = target.transform.position.x;
targetY = target.transform.position.y;

Vector2 targetPos = new Vector2(targetX,targetY );

transform.LookAt(targetPos);

rigidbody2D.velocity= transform.forward*movementSpeed;
``````

Just want to say thank you again