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;
}
最后修改:2011年9月8日 Thursday 23:20

本脚本参考基于Unity 3.4.1f5

英文部分版权属©Unity公司所有,中文部分© Unity圣典 版权所有,未经许可,严禁转载 。