游戏作业刚好有这个,看了unity有自带的slerp函数,于是自己根据原理写了一个slerp函数,在unity中跑出来结果和自带的相同。也算是取得了成功
using UnityEngine;
using System.Collections;
public class qslerp : MonoBehaviour {
// Use this for initialization
void Myslerp(Transform From,Transform To,float speed )
{
float cosa;
Quaternion direction=new Quaternion();
cosa=From.rotation.x*To.localRotation.x+From.localRotation.y*To.localRotation.y+
From.localRotation.z*To.localRotation.z+From.localRotation.w*To.localRotation.w;
float[] to=new float[4];
to[0]=To.localRotation.x;
to[1]=To.localRotation.y;
to[2]=To.localRotation.z;
to[3]=To.localRotation.w;
if(cosa<0.0f)
{
to[0]=-to[0];
to[1]=-to[1];
to[2]=-to[2];
to[3]=-to[3];
cosa=-cosa;
}
float k0,k1;
if(cosa>0.9999f)
{
k0=1.0f-speed;
k1=speed;
}
else
{
float sina=Mathf.Sqrt (1-cosa*cosa);
float a=Mathf.Atan2