I have a 2D game where the player can move up and down on the y axis, I need the enemy to rotate to look at my player (which I have working, but probably isn't the best method) while also moving up and down with the player.
I have both of those things working, but not fully, right now whenever I move up and down, the rotation happening seems to cause the enemy to move backwards more and more with each up and down movement.
I need it to remain stationary on the x and z axis's.
Also, if anyone knows how I could make it so the enemy always wants to be slightly above the player on the y axis, that'd be awesome. Meaning, instead of locking onto the player dead straight, maybe the enemy can be up slightly by perhaps 20%.
This is the code I'm working with:
public float moveSpeed = 3.0f;
public Transform target;
private Transform myTransform;
// Use this for initialization
void Start () {
myTransform = transform;
}
// Update is called once per frame
void Update () {
Quaternion rotation = Quaternion.LookRotation(
target.transform.position - transform.position,
transform.TransformDirection(Vector3.up));
transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w);
if (target.transform.position.y < myTransform.position.y) {
myTransform.position -= myTransform.up * moveSpeed * Time.deltaTime;
}
else if (target.transform.position.y > myTransform.position.y) {
myTransform.position += myTransform.up * moveSpeed * Time.deltaTime;
}
}