Script language:

  • JS
  • C#
  • Boo
Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

Suggest a change
LookAt(target: Transform, worldUp: Vector3 = Vector3.up): void;
void LookAt(Transform target, Vector3 worldUp = Vector3.up);
def LookAt(target as Transform, worldUp as Vector3 = Vector3.up) as void

Parameters

target Object to point towards.
worldUp Vector specifying the upward direction.

Description

Rotates the transform so the forward vector points at /target/'s current position.

Then it rotates the transform to point its up direction vector in the direction hinted at by the worldUp vector. If you leave out the worldUp parameter, the function will use the world y axis. worldUp is only a hint vector. The up vector of the rotation will only match the worldUp vector if the forward direction is perpendicular to worldUp.

	// This complete script can be attached to a camera to make it 
	// continuously point at another object.
	
	// 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() {
		transform.LookAt(target);
	}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    public Transform target;
    void Update() {
        transform.LookAt(target);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	public target as Transform

	def Update() as void:
		transform.LookAt(target)

LookAt(worldPosition: Vector3, worldUp: Vector3 = Vector3.up): void;
void LookAt(Vector3 worldPosition, Vector3 worldUp = Vector3.up);
def LookAt(worldPosition as Vector3, worldUp as Vector3 = Vector3.up) as void

Parameters

worldPosition Point to look at.
worldUp Vector specifying the upward direction.

Description

Rotates the transform so the forward vector points at worldPosition.

Then it rotates the transform to point its up direction vector in the direction hinted at by the worldUp vector. If you leave out the worldUp parameter, the function will use the world y axis. worldUp is only a hint vector. The up vector of the rotation will only match the worldUp vector if the forward direction is perpendicular to worldUp.

	// Point the object at the world origin
	transform.LookAt(Vector3.zero);
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    void Example() {
        transform.LookAt(Vector3.zero);
    }
}
import UnityEngine
import System.Collections

public class ExampleClass(MonoBehaviour):

	def Example() as void:
		transform.LookAt(Vector3.zero)

Your name (optional):
Your email (optional):
Please write your suggestion here: