Aim a object at another object

How can i get a object to aim one of its axis at another object, and keep aiming when the target object move? Preferably using javscript.

Is it also possible to give the object a up aim axis. So the object stays up-rigth and dont flip when its aiming at a moving object? Also preferably using javscript.

Yes, there's a function which is designed to do exactly this:


This example (from that documentation page) does exactly what you want:

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

// Rotate the camera every frame so it keeps looking at the target
function Update() {

You can try also the pre-installed scripts found under 'Components->Camera Control'.

There is one which does the Transform.LookAt but you can define how smooth the LookAt will be.

(The scripts work also when attached to other GameObjects than Cameras.)