How can i make a game object look at another object?

I have a script located in my enemy to make him chase and attack me. The script is located in the overall character. I want to make a script in the overall character AI that makes only the characters head look at the enemy. Here is what I have so far…

function LookAtEnemy()
var lookPos = closest.transform.position - Head.transform.position;

Closest is a GameObject and it just gets the closest enemy in another function.
The Head variable is var Head : GameObject;
My function is being called by Update().
Please help! Thanks in advance!!!

Oh and when I play the script as is it gives me an error saying: NullReferenceException: Object reference not set to an instance of an object.

What does that mean?

Here’s a function where you can supply a Vector3, ie the gameObject.transform.position

protected void rotateTowards(Vector3 to) {

	Quaternion _lookRotation = 
		Quaternion.LookRotation((to - transform.position).normalized);
	//over time
	transform.rotation = 
		Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * turn_speed);
	transform.rotation = _lookRotation;

You probably have a trigger to set off this event, if that trigger gives you the player’s game object, you can use:


to automatically rotate the game object to point at the player.

i use this :

 Vector3 dir = target.position - transform.position;
    Quaternion lookRot = Quaternion.LookRotation(dir);
    lookRot.x = 0; lookRot.z = 0;
    transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, Mathf.Clamp01(3.0f * Time.maximumDeltaTime));

I found this…
It makes the camera look at the object selected with GameObject.Find();

if (Input.GetKey(KeyCode.L)) {
    GameObject obj = GameObject.Find("CameraAt");