欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作

 更新時(shí)間:2021年04月12日 15:55:20   作者:風(fēng)中追風(fēng)3zZ  
這篇文章主要介紹了Unity之繞軸進(jìn)行旋轉(zhuǎn)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

先上一張效果圖

using UnityEngine;
using System.Collections;
public class TestRotateRound : MonoBehaviour
{
    public GameObject Sphere;
    private float curtTime = 0.0f;
    void Update()
    {
        //使用C#封裝好的代碼RotateAround
        gameObject.transform.RotateAround(Sphere.transform.position, Sphere.transform.up, 72 * Time.deltaTime);
        //自己封裝代碼,功能和上面的相同
        //RotateAround(Sphere.transform.position,Vector3.up, 72 * Time.deltaTime);
    }
    private void RotateAround(Vector3 center, Vector3 axis, float angle)
    {
        //繞axis軸旋轉(zhuǎn)angle角度
        Quaternion rotation = Quaternion.AngleAxis(angle, axis);
        //旋轉(zhuǎn)之前,以center為起點(diǎn),transform.position當(dāng)前物體位置為終點(diǎn)的向量.
        Vector3 beforeVector = transform.position - center;
        //四元數(shù) * 向量(不能調(diào)換位置, 否則發(fā)生編譯錯(cuò)誤)
        Vector3 afterVector = rotation * beforeVector;//旋轉(zhuǎn)后的向量
        //向量的終點(diǎn) = 向量的起點(diǎn) + 向量
        transform.position = afterVector + center;
        //看向Sphere,使Z軸指向Sphere
        transform.LookAt(Sphere.transform.position);
    }
}

補(bǔ)充:Unity繞x軸旋轉(zhuǎn)并限制角度的陷阱

在制作FPS相機(jī)時(shí),遇到了需要限制角度的需求,視角只能查看到-60到60度的范圍,而在Unity的Transform組件中,繞x軸逆時(shí)針旋轉(zhuǎn),Transform組件的localEulerAngle會(huì)在0~360范圍內(nèi)遞增(如圖)

關(guān)鍵在于其中的角度轉(zhuǎn)換,直接上代碼

        public static void RotateClampX(this Transform t, float degree, float min, float max)
        {
            degree = (t.localEulerAngles.x - degree);
            if (degree > 180f)
            {
                degree -= 360f;
            }
            degree = Mathf.Clamp(degree, min, max);
            t.localEulerAngles = t.localEulerAngles.SetX(degree);
        }

補(bǔ)充:Unity3D 實(shí)現(xiàn)物體始終面向另一個(gè)物體(繞軸旋轉(zhuǎn)、四元數(shù)旋轉(zhuǎn))

一開(kāi)始本人糾結(jié)于在VR中,怎么利用手柄來(lái)控制物體的旋轉(zhuǎn),物體位置不變。

相當(dāng)于:地球儀。更通俗點(diǎn)來(lái)說(shuō),就是一個(gè)棍子插到地球儀上,然后拿著棍子就可以控制地球儀轉(zhuǎn)。手柄相當(dāng)于那根棍子。

代碼如下:

myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);

這句代碼實(shí)現(xiàn)了 myTransform 始終可以根據(jù) target 旋轉(zhuǎn),rotationSpeed控制速度。

當(dāng)然上面這句話(huà)僅僅只是始終面向,還沒(méi)有加上一開(kāi)始記錄下target的初始旋轉(zhuǎn)。不然一開(kāi)始就要跟著手柄轉(zhuǎn),而不是自己隨意控制。對(duì)于上句的理解,我理解完便貼上。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

最新評(píng)論