Making a specific axis look at an object

How can i get something to look at something but just on one axis, such as the y axis, so the object is always looking at an object but never looking up or down, just left and right, always following the object. I tried this code and it didnt work:

var PlayerObj : Transform;

function Update () {

transform.rotation.y.LookAt(PlayerObj);

}

Please help!

C#

using UnityEngine;
using System.Collections;
public class Yrotater : MonoBehaviour
{
	public Transform lookAt;
	void Update()
	{
		Vector3 worldLookDirection = lookAt.position - transform.position;
		Vector3 localLookDirection = transform.InverseTransformDirection(worldLookDirection);
		localLookDirection.y = 0;
		transform.forward = transform.rotation * localLookDirection;
	}
}

You could try to make 2 vector3’s with the same x and z position and than use the LookAt function on Quaternion. Than it will rotate on only the y axis. Or you could make a new quaternion, set it equal to the current rotation, then rotate the object and reset everything in the wuaternion exept for the y rotation. Only problem is, quaterions are realy difficult to understand so if you use the second method, and you run into a problem, try the first :slight_smile:
Hope this helps!