I’m wanting to have an enemy game object rotate on the Y axis toward the player, can this be done using a Vector2 that calculates the player’s x and z positions or is there another way to do this? And how would I wrap this into a 360 degree calculation?
In case others were wondering how to perform lookat on the y axis only heres a sample code
using UnityEngine;
using System.Collections;
public class rotationTest : MonoBehaviour {
private GameObject player;
// Use this for initialization
void Start () {
player = GameObject.Find (“player”);
}
// Update is called once per frame
void Update () {
Vector3 offset = new Vector3 (player.transform.position.x, transform.position.y, player.transform.position.z);
transform.LookAt(offset);
}
}
Just make sure to indicate in the new Vector 3 to follow player X and Z (but follow it’s own y position)
The current rotation is stored in your GameObject’s Transform.rotation. To rotate by any angle in any plane, you multiply a Quaternion on top of it. For example:
// represents a rotation of 90 degrees about the Y axis
Quaternion ninetyDegrees = Quaternion.Euler(0f, 90f, 0f);
// rotate another 90 degrees
transform.rotation *= ninetyDegrees;