Unity 3D Game Engine – LookAt Object

Inside Hierarchy create:

1. Sphere and attach MotionScript.js


#pragma strict

public var speed : float = 3f;


function Update () 
{
    transform.Translate(-Input.GetAxis("Horizontal") * speed * Time.deltaTime, 0, 0);
}

2. Cube and attach LookAtScript.js


#pragma strict

public var target : Transform;

function Update () 
{
    var relativePos : Vector3 = target.position - transform.position;
    transform.rotation = Quaternion.LookRotation(relativePos);
}

Inspector> LookAtScript.js> DRAG AND DROP Sphere over var target

3. Move the Sphere using keyboard arrow, the Cube will look at the Sphere