Im making a simple free roam spaceship game, and all i need is the alien to move towards me, i only know how to do this in 3d, ive sorted out the rotation bit(with help) so it always points towards me, but how can I make it move towards me, because at the movement im telling it to go foward , but our Z angles are equal?? Can i make it so that it follows my X+Y?? Just Look :
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
// Use this for initialization
void Awake() {
myTransform = transform;
}
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Update is called once per frame
void Update () {
Vector3 dir = target.position - myTransform.position;
dir.z = 0.0f; // Only needed if objects don't share 'z' value
if (dir != Vector3.zero) {
myTransform.rotation = Quaternion.Slerp (myTransform.rotation,
Quaternion.FromToRotation(Vector3.right, dir), rotationSpeed * Time.deltaTime);
}
//Move Towards Target
myTransform.position += myTransform.position * moveSpeed * Time.deltaTime;
}
}