Unity3D中Quaternion类SetLookRotation方法和LookRotation方法使用解析

8.2.2 SetLookRotation方法:设置Quaternion实例的朝向

基本语法:(1)public void SetLookRotation(Vector3 view);

(2)public void SetLookRotation(Vector3 view, Vector3 up);

功能说明:此方法的功能是用来对一个Quaternion实例的朝向进行设置。设有如下代码:

Quaternion q1 = Quaternion.identity;

q1.SetLookRotation(v1, v2);

transform.rotation = q1;

则:

(1)transform.forward方向与v1方向相同;

(2)transform.right垂直于由Vector3.zero、v1和v2三点构成的平面;

(3)v2的作用除了与Vector3.zero和v1构成平面来决定transform.right的方向外,还用来决定transform.up的朝向,因为当transform.forward和transform.right方向确定后,transform.up方向剩下两种可能,到底选用哪一种便由v2来影响,transform.up方向的选取方式总会使得transform.up的方向和v2的方向的夹角小于或等于90度。当然,一般情况下v2.normalized和transform.up是不相同的。

(4)当v1为Vector3.zero时方法失效,即不要在使用此方法时把v1设置成Vector3.zero。

提示:不可以直接使用transform.rotation.SetLookRotation(v1,v2)的方式来使用SetLookRotation方法,否则会不起作用。应该使用上述代码所示的方式,首先实例化一个Quaternion,然后对其使用SetLookRotation,最后将其赋给transform.rotation。

实例演示:下面通过实例演示方法SetLookRotation的使用。

using UnityEngine;
using System.Collections;

public class SetLookRotation_ts : MonoBehaviour
{
    public Transform A, B, C;
    Quaternion q1 = Quaternion.identity;

    void Update()
    {
        //不可直接使用C.rotation.SetLookRotation(A.position,B.position);
        q1.SetLookRotation(A.position, B.position);
        C.rotation = q1;
        //分别绘制A、B和C.right的朝向线
        //请在Scene视图中查看
        Debug.DrawLine(Vector3.zero, A.position, Color.red);
        Debug.DrawLine(Vector3.zero, B.position, Color.green);
        Debug.DrawLine(C.position, C.TransformPoint(Vector3.right * 2.5f), Color.yellow);
        Debug.DrawLine(C.position, C.TransformPoint(Vector3.forward * 2.5f), Color.gray);
        //分别打印C.right与A、B的夹角
        Debug.Log("C.right与A的夹角:" + Vector3.Angle(C.right, A.position));
        Debug.Log("C.right与B的夹角:" + Vector3.Angle(C.right, B.position));
        //C.up与B的夹角
        Debug.Log("C.up与B的夹角:" + Vector3.Angle(C.up, B.position));
    }
}

在这段代码中,首先声明了三个GameObject类型的变量A、B和C,然后在Update方法中对q1调用方法SetLookRotation,并将改变后的q1赋给C.rotation,注意不可直接使用C.rotation. SetLookRotation (A.position,B.position)来设置C的rotation。最后在Scene场景中绘制直线以便更好的观察。请读者自行运行程序在Scene视图中查看,在程序运行时可以试着更改A或B的位置查看C的状态变化。图8-2是一张运行时截图及注释,图8-3是Debug的输出结果。




8.3.7 LookRotation方法:设置Quaternion的朝向

基本语法:(1)public static Quaternion LookRotation(Vector3 forward);

(2)public static Quaternion LookRotation(Vector3 forward, Vector3upwards);

其中参数forward为返回Quaternion的forward朝向。

功能说明:此方法用于返回一个Quaternion实例,使GameObject对象的Z轴朝向参数forward方向。此方法与方法SetLookRotation(view : Vector3, up : Vector3 = Vector3.up)功能相同,只是用法上有些不同。

实例演示:下面通过实例演示方法LookRotation的使用。

using UnityEngine;
using System.Collections;

public class LookRotation_ts : MonoBehaviour
{
    public Transform A, B, C, D;
    Quaternion q1 = Quaternion.identity;

    void Update()
    {
        //使用实例方法
        //不可直接使用C.rotation.SetLookRotation(A.position,B.position);
        q1.SetLookRotation(A.position, B.position);
        C.rotation = q1;
        //使用类方法
        D.rotation = Quaternion.LookRotation(A.position, B.position);
        //绘制直线,请在Scene视图中查看
        Debug.DrawLine(Vector3.zero, A.position, Color.white);
        Debug.DrawLine(Vector3.zero, B.position, Color.white);
        Debug.DrawLine(C.position, C.TransformPoint(Vector3.up * 2.5f), Color.white);
        Debug.DrawLine(C.position, C.TransformPoint(Vector3.forward * 2.5f), Color.white);
        Debug.DrawLine(D.position, D.TransformPoint(Vector3.up * 2.5f), Color.white);
        Debug.DrawLine(D.position, D.TransformPoint(Vector3.forward * 2.5f), Color.white);
    }
}

在这段代码中,首先声明了4个Transform变量用于指向不同的对象,然后在Update方法中调用方法SetLookRotation使得C的Z轴方向与向量A相同,Y轴方向尽量接近向量B。接着调用静态方法LookRotation使得D的Z轴方向与向量A相同,Y轴方向尽量接近向量B,如图8-9所示。请读者自行运行程序在Scene视图而非Game视图中查看。程序运行时可以在Scene视图中试着拖动A或B的位置观察C和D的变化。


本文章摘自图书《Unity API解析》,源码下载地址:http://www.ituring.com.cn/book/1474

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值