Hey all just a quick question about rotating a game object on the y.

(C#)

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?

Thanks again.

Perhaps you may want to use Transform.LookAt()?

OMG!!! I wish I would have known that was there lol my bad I just Started C# and unity like 2 weeks ago so I’m still kinda learning the ropes.

Thanks so much!!!

now to figure out how to add 90 degrees to that

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;

Thanks again, blizzy this is a very helpful trick when dealing with quaternions now everything works great!!!

and I must say that I never would have guessed about the *= thing so that’s something to remember