- Home /
2D enemy AI
Hi, I'm currently working on a 2d game but I'm stuck on the enemy AI.I want it to go to the target which the player. I've shearched on the internet but there wasn't any tutorials. I've tried myself but I have a problem with transform.LookAt() which isn't for 2D. Here is my script :
var Player : Transform;
var Speed = 4;
var Dist = 5;
function Start ()
{
}
function Update ()
{
transform.LookAt(Player.transform.position);
if(Vector3.Distance(transform.position,Player.position) >= Dist)
{
transform.position += transform.forward*Speed*Time.deltaTime;
}
}
Thanks for you help.
Check this tutorial out https://www.youtube.com/watch?v=CWx$$anonymous$$LkJUxbo its in c# but it shows you how to rotate a 2d object towards a target.
Answer by Julien-Lynge · Mar 08, 2015 at 09:40 PM
If you're working in 2D, one solution is to set the rotation of the object yourself. You'll need to use the trigonometric methods (like Sine and Cosine) to set the rotation of your object.
Another solution is to make your gameObject the child of another gameObject, and rotate it 90 degrees from the parent. You can have the parent look at the target object, and the child will still face toward your camera.
If you don't understand what those things mean, you probably need to follow some Unity tutorials and learn a bit more about how to use Unity.