旋转插值运算

// 摘要:
//     Creates a rotation with the specified forward and upwards directions.
//
// 参数:
//   forward:
//     The direction to look in.
//
//   upwards:
//     The vector that defines in which direction up is.

例子:

using UnityEngine;
using System.Collections;
/*
* 
*/
public class ExampleClass : MonoBehaviour {
    public Transform player;
    public Transform target;
    void Update() {
          Vector3 relativePos = target.position - player.position;
          //relativePos.y = 0;//player物体朝向target物体时默认y轴会有倾斜,如果不想倾斜,可设置y轴坐标为0
          player.rotation = Quaternion.LookRotation(relativePos);
    }
}

**仅仅使用Quaternion中的LookRotation方法,可以使当前物体在一瞬间就朝向目标物体。假使要物体慢慢旋转朝向目标物体,就需要再加上插值运算:

Quaternion.Lerp或者Quaternion.Slerp

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    pullic Transform player;
    public Transform target;
    public float speed = 5.0f;
    
    void Update() {
        Vector3 relativePos = target.position - player.position;
        Quaternion targetRotation = Quaternion.LookRotation(relativePos);

        //物体想慢慢朝向目标
        player.rotation = Quaternion.Slerp(player.rotation, targetRotation, Time.time * speed);
        
        //物体慢慢到达目标位置
        player.position = Quaternion.Lerp(player.position, target.position, Time.time * speed);
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值