Unity實現(xiàn)物體沿自身的任意軸向旋轉(zhuǎn)
更新時間:2020年01月19日 11:15:35 作者:xiaochenXIHUA
這篇文章主要為大家詳細介紹了Unity實現(xiàn)物體沿自身的任意軸向旋轉(zhuǎn),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Unity實現(xiàn)物體沿任意軸向旋轉(zhuǎn),供大家參考,具體內(nèi)容如下
一、創(chuàng)建一個需要旋轉(zhuǎn)的物體

二、編寫控制該物體的腳本
using UnityEngine;
using System.Collections;
public class Test_ElectricFan : MonoBehaviour
{
public bool isOpen=false; //是否開始旋轉(zhuǎn)
public int speed=2; //旋轉(zhuǎn)的速度
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if(isOpen)
{
RotateAxisOfSelf(SelfAxis.Y,speed);
}
}
/// <summary>
/// 讓物體繞自身的軸旋轉(zhuǎn)
/// </summary>
/// <param name="AxisX">自身的軸</param>
private void RotateAxisOfSelf(SelfAxis selfAxis,int speed=2)
{
switch(selfAxis)
{
case SelfAxis.X:
this.transform.Rotate (new Vector3(1*Time.deltaTime*speed,0,0));
break;
case SelfAxis.Y:
this.transform.Rotate (new Vector3(0,1*Time.deltaTime*speed,0));
break;
case SelfAxis.Z:
this.transform.Rotate (new Vector3(0,0,1*Time.deltaTime*speed));
break;
default:
this.transform.Rotate (new Vector3(1*Time.deltaTime*speed,0,0));
break;
}
}
//枚舉軸
enum SelfAxis
{
X,
Y,
Z,
}
}
三、將編寫好的控制物體的腳本添加給需要沿自身任意軸旋轉(zhuǎn)的物體上,然后運行程序,接著點擊IsOpen打鉤此時物體開始旋轉(zhuǎn)

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 中的委托與事件實現(xiàn)靈活的回調(diào)機制(應(yīng)用場景分析)
委托提供了一種類型安全的方式將方法作為參數(shù)傳遞,而事件則允許對象通知其他對象發(fā)生了某些事情,這篇文章主要介紹了C# 中的委托與事件實現(xiàn)靈活的回調(diào)機制,需要的朋友可以參考下2024-12-12
WPF利用WindowChrome實現(xiàn)自定義窗口
這篇文章主要為大家詳細介紹了WPF如何利用WindowChrome實現(xiàn)自定義窗口,文中的示例代碼講解詳細,具有一定的借鑒價值,需要的可以參考一下2023-02-02

