unity 3d 中的quaternion.Slerp的作用以及用法

using UnityEngine;
using System.Collections;

public class enemyAI : MonoBehaviour {
	public Transform target;
	public int moveSpeed;
	public int rotationSpeed;
	private Transform myTransform;

	void Awake() {
		myTransform = transform;
	}
	// Use this for initialization
	void Start () {
		GameObject go = GameObject.FindGameObjectWithTag ("Player");
		target = go.transform;
	}
	
	// Update is called once per frame
	void Update () {
		Debug.DrawLine (target.transform.position, myTransform.position, Color.yellow);
		// look at target
		myTransform.rotation = Quaternion.Slerp (myTransform.rotation,Quaternion.LookRotation(target.position-myTransform.position),
		                                         rotationSpeed*Time.deltaTime);

		// move to target
		myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
	}
}



忽略掉什么叫做四元数平滑插值这种复杂的概念,这个方法的意思其实就是:让初始点面朝目标点。第三个参数的意思是让初始点转多少度才能面朝目标点。参数意思是从from 的rotation慢慢旋转增值到to.rotation.以多少的角度旋转,把其中的值插到这个中间过程中,就是这个方法所要表达的意思。


官方文档上这么写的:

static function Slerp (from : Quaternion, to : Quaternion, t : float) : Quaternion

Description描述

Spherically interpolates from towards to by t.

球形插值,通过t值from向to之间插值。

C#JavaScript
// Interpolates rotation between the rotations
// of from and to.
// (Choose from and to not to be the same as
// the object you attach this script to)
//在from和to之间插值旋转.
//(from和to不能与附加脚本的物体相同)
var from : Transform;
var to : Transform;
var speed = 0.1;
function Update () {
	transform.rotation = Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);
}


你可能感兴趣的:(unity,3D,Slerp,Quaternion)