Unity 3D Game Engine – JS Script – LookAt Camera Target

1. MAIN TOP MENU> Game Object> Create Empty> Inspector> rename it ‘CameraTarget’

2. Hierarchy> Select ‘Main Camera’> Inspector> ‘Add Component’> New script> ‘LookAt.js’

LookAt.js:


#pragma strict

// #################################################################################################
// This complete script can be attached to a camera to make it continuously point at another object.
// ################################################################################################# 

// Camera target public variable START #############################################################
// The target variable shows up as a property in the inspector. Drag another object onto it to make the camera look at it.
var cameraTarget : Transform;
// Camera target public variable END ###############################################################

function Start () {

}
	
function Update() {
	
}
	
function LateUpdate () {
//LateUpdate is called after all Update functions have been called. 
// This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.
transform.LookAt(cameraTarget);
}

3. Hierarchy> ‘Main Camera’> Inspector> LookAt.js> DRAG ANd DROP over puiblic variable ‘Camera Target’> the ‘CameraTarget’ empty object you have created at point 1.

4. PLAY the game, while playing from Inspector change ‘CameraTarget’ object position to see the final result.