Set Rotation

Hello, I am writing a C# script to make a farmer face a gameobject. However, I only want the farmer to pivot on the y axis. So far my script is:

using UnityEngine;
using System.Collections;

public class choose_mole : MonoBehaviour {



	private static int recieverclosestsq;

	public Transform target;
	public Vector3 relativepos;


	void Update () 
	{



			recieverclosestsq = molepos.closestsq;


			Vector3 relativePos = target.position - transform.position;
			Quaternion rotation = Quaternion.LookRotation(relativePos);
			transform.rotation = rotation;

		
	}
}

I can’t find anything useful that locks the x and z rotations. Please send in a detailed explanation if you can. Thank you for the help!

Use a new Vector for your target vector with the same Y as the Farmer.

Vector3 nTarget = new Vector3(target.position.x, transform.position.y, target.position.z);

Ant you could use transform.LookAt()

Vector3 nTarget = new Vector3(target.position.x, transform.position.y, target.position.z);
transform.LookAt(nTarget);