Quaternion.LookRotation 注视旋转

static function LookRotation (forward : Vector3, upwards : Vector3 = Vector3.up) : Quaternion

Description描述

Creates a rotation that looks along forward with the the head upwards along upwards

创建一个旋转,沿着forward(z轴)并且头部沿着upwards(y轴)的约束注视。也就是建立一个旋转,使z轴朝向view  y轴朝向up。

Logs an error if the forward direction is zero.

如果forward方向是0,记录一个错误。

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
	public Transform target;
	void Update() {
		Vector3 relativePos = target.position - transform.position;
		Quaternion rotation = Quaternion.LookRotation(relativePos);
		transform.rotation = rotation;
	}
}
// Most of the time you can use:
// transform.LookAt instead
//大多数时间你可以使用transform.LookAt代替
var target : Transform;
function Update () {
	var relativePos = target.position - transform.position;
	var rotation = Quaternion.LookRotation(relativePos);
	transform.rotation = rotation;
}
Page last updated: 2011-9-8