国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Unity 制作旋轉(zhuǎn)門 推拉門 柜門 抽屜 點擊自動開門效果 開關(guān)門自動播放音效 (附帶編輯器擴展代碼)

這篇具有很好參考價值的文章主要介紹了Unity 制作旋轉(zhuǎn)門 推拉門 柜門 抽屜 點擊自動開門效果 開關(guān)門自動播放音效 (附帶編輯器擴展代碼)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

簡易的門制作

對于一個新手來說,這個工具是最好的選擇
Unity 制作旋轉(zhuǎn)門 推拉門 柜門 抽屜 點擊自動開門效果 開關(guān)門自動播放音效 (附帶編輯器擴展代碼)

上一篇關(guān)于開關(guān)門的文章相對于復(fù)雜,感興趣的可以查看上篇開關(guān)門制作

優(yōu)點
  • 掛載就能使用
  • 控制面板一看就懂(全是中文)
  • 簡單的調(diào)試就能獲得自己想要的效果
  • 易懂且易修改的代碼
面板

Unity 制作旋轉(zhuǎn)門 推拉門 柜門 抽屜 點擊自動開門效果 開關(guān)門自動播放音效 (附帶編輯器擴展代碼)

參數(shù)
  • 鎖:勾選后不能對門進(jìn)行操作
  • 聲音:激活時自動播放
  • 激活:測試開關(guān)門
  • 查看結(jié)果位置:按住查看結(jié)果,松開返回
    Unity 制作旋轉(zhuǎn)門 推拉門 柜門 抽屜 點擊自動開門效果 開關(guān)門自動播放音效 (附帶編輯器擴展代碼)
    旋轉(zhuǎn)門的使用不做介紹
推拉門(推拉抽屜):
  • 起始位置:門的初始位置
  • 結(jié)束位置:門要移動的位置
  • 得到位置按鈕:當(dāng)你在場景中調(diào)整門的位置后,把當(dāng)前門的位置賦值到左側(cè)
代碼

掛載之后會自動添加Audio Source組件

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(AudioSource))]
public class Door_Interaction : MonoBehaviour
{
    public enum doorType { RotatingDoor = 0, SlidingDoor = 1 }
    public doorType doorMovementType;
    public bool open;
    public bool locked;
    public float rotateAngle = 90;
    public float speed = 1;
    public enum axis { X = 0, Y = 1, Z = 2 }
    public axis rotateAxis = axis.Y;
    public Vector3 startingPosition;
    public Vector3 endingPosition;
    public AudioClip startClip;
    public AudioClip endClip;
    private AudioSource sound;
    public Quaternion originRotation;
    public Quaternion openRotation;
    private float r;
    private bool opening;

    [Tooltip("激活測試")]
    public bool activate = false;
    void Start()
    {
        InitRotation();
        sound = GetComponent<AudioSource>();
        r = 0;
    }
    public void InitRotation()
    {
        originRotation = transform.rotation;
        openRotation = originRotation;
        if (rotateAxis == axis.Y)
        {
            openRotation = Quaternion.Euler(new Vector3(originRotation.eulerAngles.x, originRotation.eulerAngles.y + rotateAngle, originRotation.eulerAngles.z));
        }
        else if (rotateAxis == axis.X)
        {
            openRotation = Quaternion.Euler(new Vector3(originRotation.eulerAngles.x + rotateAngle, originRotation.eulerAngles.y, originRotation.eulerAngles.z));
        }
        else if (rotateAxis == axis.Z)
        {
            openRotation = Quaternion.Euler(new Vector3(originRotation.eulerAngles.x, originRotation.eulerAngles.y, originRotation.eulerAngles.z + rotateAngle));
        }
    }

    void Update()
    {
        if (activate)
        {
            activate = false;
            if (opening == false && locked == false)
            {
                open = !open;
                r = 0;
                opening = true;
            }
        }
        if (opening)
        {
            ChangeState(open);
        }
    }
    void ChangeState(bool State)
    {
        Quaternion quaternion;
        Vector3 vector3;
        if (State)
        {
            quaternion = openRotation;
            vector3 = endingPosition;
            if (startClip!=null&& r==0)
            {
                sound.clip = startClip;
                sound.Play();
            }
            
        }
        else
        {
            quaternion = originRotation;
            vector3 = startingPosition;
            if (endClip != null && r == 0)
            {
                sound.clip = endClip;
                sound.Play(); 
            }
        }
        r += Time.deltaTime * speed;
        if (doorMovementType == doorType.RotatingDoor)
        {
            if (Quaternion.Angle(quaternion, transform.rotation) > 0.1F)
            {
                transform.rotation = Quaternion.Slerp(transform.rotation, quaternion, r);
            }
            else
            {
                transform.rotation = quaternion;
                r = 0;
                opening = false;
            }
        }
        else if (doorMovementType == doorType.SlidingDoor)
        {
            if (Vector3.Distance(transform.localPosition, vector3) > 0.005F)
            {
                transform.localPosition = Vector3.Lerp(transform.localPosition, vector3, r);
            }
            else
            {
                transform.localPosition = vector3;
                r = 0;
                opening = false;
            }
        }
    }
}
編輯器擴展代碼

需要注意!這個代碼必須放在Editor文件下才能正常使用,Unity會自己處理Editor文件夾,就像Resources文件夾一樣
我之前寫的通用相機控制器也是一樣的使用方法
感興趣的可以查看通用相機控制器
Unity 制作旋轉(zhuǎn)門 推拉門 柜門 抽屜 點擊自動開門效果 開關(guān)門自動播放音效 (附帶編輯器擴展代碼)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Door_Interaction)), CanEditMultipleObjects]
public class Door_Interaction_Editor : Editor
{
    private SerializedProperty open;
    private SerializedProperty locked;
    private SerializedProperty rotateAngle;
    private SerializedProperty speed;
    private SerializedProperty rotateAxis;
    private SerializedProperty startingPosition;
    private SerializedProperty endingPosition;
    private SerializedProperty activate;
    private SerializedProperty startClip;
    private SerializedProperty endClip;
    private GUIStyle defaultStyle = new GUIStyle();

    public void OnEnable()
    {
        open = serializedObject.FindProperty("open");
        locked = serializedObject.FindProperty("locked");
        rotateAngle = serializedObject.FindProperty("rotateAngle");
        speed = serializedObject.FindProperty("speed");
        rotateAxis = serializedObject.FindProperty("rotateAxis");
        startingPosition = serializedObject.FindProperty("startingPosition");
        endingPosition = serializedObject.FindProperty("endingPosition");
        activate = serializedObject.FindProperty("activate");
        startClip = serializedObject.FindProperty("startClip");
        endClip = serializedObject.FindProperty("endClip");
    }
    bool IsButtonDown = false;
    Vector3 CurrentPosition= Vector3.zero;
    Quaternion CurrentRotation = Quaternion.identity;
    public override void OnInspectorGUI()
    {
        serializedObject.Update();
        var myScript = target as Door_Interaction;
        var doorGUIContent = new GUIContent("門類型", "選擇不同門的類型參數(shù)也有所改變");
        GUIContent[] doorOptions;
        doorOptions = new[] { new GUIContent("旋轉(zhuǎn)門"), new GUIContent("推拉門") };

        myScript.doorMovementType = (Door_Interaction.doorType)EditorGUILayout.Popup(doorGUIContent, (int)myScript.doorMovementType, doorOptions);
        EditorGUILayout.PropertyField(open,new GUIContent("當(dāng)前狀態(tài)"));
        EditorGUILayout.PropertyField(locked, new GUIContent("鎖"));
        EditorGUILayout.PropertyField(speed, new GUIContent("執(zhí)行速度"));

        EditorGUILayout.PropertyField(startClip, new GUIContent("打開時的聲音"));
        EditorGUILayout.PropertyField(endClip, new GUIContent("關(guān)閉時的聲音"));
        EditorGUILayout.PropertyField(activate, new GUIContent("激活"));

        GUILayout.Space(20);//間隔
        if (myScript.doorMovementType == Door_Interaction.doorType.RotatingDoor) {

            EditorGUILayout.PropertyField(rotateAngle, new GUIContent("旋轉(zhuǎn)角度"));
            EditorGUILayout.PropertyField(rotateAxis, new GUIContent("軸", "門的旋轉(zhuǎn)軸。 默認(rèn)是Y "));
            GUILayout.Space(20);//間隔
            if (GUILayout.RepeatButton(new GUIContent("查看結(jié)束位置", "在視圖中查看結(jié)束位置")))
            {
                if (IsButtonDown == false)
                {
                    IsButtonDown = true;
                    myScript.InitRotation();
                    CurrentRotation = myScript.transform.localRotation;
                }
                myScript.transform.rotation = myScript.openRotation;
            }
            else
            {
                if (IsButtonDown)
                {
                    IsButtonDown = false;
                    myScript.transform.localRotation = CurrentRotation;
                }
            }
        } else if (myScript.doorMovementType == Door_Interaction.doorType.SlidingDoor) {

            defaultStyle.alignment = TextAnchor.UpperCenter; //字體對齊方式: 水平靠左,垂直居中
            defaultStyle.normal.textColor = Color.yellow; //字體顏色:黃色
            defaultStyle.fontSize = 15; //字體大?。?20

            GUILayout.Label("起始位置", defaultStyle);
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PropertyField(startingPosition, new GUIContent(""));
            if (GUILayout.Button(new GUIContent("得到位置", "將當(dāng)前的局部坐標(biāo)賦值給開始位置")))
            {
                myScript.startingPosition = myScript.transform.localPosition;
            }
            EditorGUILayout.EndHorizontal();

            GUILayout.Label("結(jié)束位置", defaultStyle);
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PropertyField(endingPosition, new GUIContent(""));
            if (GUILayout.Button(new GUIContent("得到位置", "將當(dāng)前的局部坐標(biāo)賦值給結(jié)束位置")))
            {
                myScript.endingPosition = myScript.transform.localPosition;
            }
            EditorGUILayout.EndHorizontal();

            GUILayout.Space(20);//間隔
            if (GUILayout.RepeatButton(new GUIContent("查看結(jié)束位置", "在視圖中查看結(jié)束位置")))
            {
                if (IsButtonDown==false)
                {
                    IsButtonDown = true;
                    CurrentPosition = myScript.transform.localPosition;
                }
                myScript.transform.localPosition = myScript.endingPosition;
            }
            else
            {
                if (IsButtonDown)
                {
                    IsButtonDown = false;
                    myScript.transform.localPosition = CurrentPosition;
                }
            } 
        }
        serializedObject.ApplyModifiedProperties();//顧名思義 應(yīng)用修改的屬性
        if (GUI.changed == true) {
            EditorUtility.SetDirty(target);//這個函數(shù)告訴引擎,相關(guān)對象所屬于的Prefab已經(jīng)發(fā)生了更改。
        }
    }
}

別要Demo了,按照流程賦值粘貼代碼就能用 !文章來源地址http://www.zghlxwxcb.cn/news/detail-424166.html

到了這里,關(guān)于Unity 制作旋轉(zhuǎn)門 推拉門 柜門 抽屜 點擊自動開門效果 開關(guān)門自動播放音效 (附帶編輯器擴展代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進(jìn)行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Html 3D旋轉(zhuǎn)相冊制作

    Html 3D旋轉(zhuǎn)相冊制作

    ?程序示例精選 Html 3D旋轉(zhuǎn)相冊制作 如需安裝運行環(huán)境或遠(yuǎn)程調(diào)試,見文章底部微信名片,由專業(yè)技術(shù)人員遠(yuǎn)程協(xié)助! 這篇博客針對Html 3D旋轉(zhuǎn)相冊制作編寫代碼,代碼整潔,規(guī)則,易讀。 學(xué)習(xí)與應(yīng)用推薦首選。 ? ????????一、所需工具軟件 ????????二、使用步驟 ?

    2024年02月12日
    瀏覽(25)
  • 【Python】使用Print函數(shù)制作旋轉(zhuǎn)的動畫

    【Python】使用Print函數(shù)制作旋轉(zhuǎn)的動畫

    如果你想有效地學(xué)習(xí)Python,這篇文章可能不適合你。接下來的一切都可能是愚蠢、和浪費時間,但哪有怎么樣,畢竟這玩意很有趣呀! 首先,我們來看兩個好玩的Python腳本,如下: 在終端中運行上述腳本,可以得到結(jié)果如下: 在來看第二個腳本,如下所示: 在終端中,運行

    2024年02月04日
    瀏覽(25)
  • yolov5旋轉(zhuǎn)目標(biāo)框的數(shù)據(jù)集標(biāo)簽制作

    yolov5旋轉(zhuǎn)目標(biāo)框的數(shù)據(jù)集標(biāo)簽制作

    1.rolabelimg標(biāo)注 https://blog.csdn.net/qq_42921511/article/details/127619447 2. roxml轉(zhuǎn)為dota格式的txt roxml文件格式: dota的txt格式: 修改完路徑后運行roxml_to_dota.py即可 3. dota格式txt轉(zhuǎn)為yolo格式的txt標(biāo)簽 ** 環(huán)境配置: python=3.7 pip install opencv-python==4.1.2.30 (ps:必須為這個版本) 圖片:長和寬一致的

    2024年02月04日
    瀏覽(22)
  • 【UE4 塔防游戲系列】05-制作可跟蹤旋轉(zhuǎn)的炮塔

    【UE4 塔防游戲系列】05-制作可跟蹤旋轉(zhuǎn)的炮塔

    目錄 效果 步驟 一、設(shè)置游戲觀察視角? 二、設(shè)置PlayerController 三、制作可跟蹤旋轉(zhuǎn)的炮塔 ? 一、設(shè)置游戲觀察視角? 在視口中調(diào)整好位置,能夠看到敵人行走的全部路線即可。然后在此處創(chuàng)建CameraActor ?打開關(guān)卡藍(lán)圖,設(shè)置使用這個相機的視角 ?在世界場景設(shè)置中,默認(rèn)的

    2024年02月16日
    瀏覽(31)
  • Unity中Shader旋轉(zhuǎn)矩陣(二維旋轉(zhuǎn)矩陣)

    Unity中Shader旋轉(zhuǎn)矩陣(二維旋轉(zhuǎn)矩陣)

    在Shader中,我們經(jīng)常對頂點進(jìn)行旋轉(zhuǎn)變換。我們在這篇文章中了解一下旋轉(zhuǎn)使用的旋轉(zhuǎn)矩陣。 我們先在2D平面下,了解2D原理 我們需要求的就是坐標(biāo)系旋轉(zhuǎn)后,P點在旋轉(zhuǎn)后坐標(biāo)系中的位置。 因為,我們旋轉(zhuǎn)物體時,我們是圍繞著一個軸旋轉(zhuǎn)。 旋轉(zhuǎn)后,我們需要求的就是物體

    2024年02月04日
    瀏覽(22)
  • Unity中Shader旋轉(zhuǎn)矩陣(四維旋轉(zhuǎn)矩陣)

    Unity中Shader旋轉(zhuǎn)矩陣(四維旋轉(zhuǎn)矩陣)

    在上篇文章中,我們推算出了Shader物體旋轉(zhuǎn)所使用的二維旋轉(zhuǎn)矩陣。 Unity中Shader旋轉(zhuǎn)矩陣(二維旋轉(zhuǎn)矩陣) 在這篇文章中,我們來推算得到四維旋轉(zhuǎn)矩陣。 圍繞X軸旋轉(zhuǎn)代表,物體頂點的X軸不變。 我們把P 2 增加一維且分量為1 M rotate * P 1 = P 2 M rotate = P 2 * P 1 -1 最后得到M ro

    2024年02月04日
    瀏覽(20)
  • ThreeJS 炫酷特效旋轉(zhuǎn)多面體Web頁 Demo 01《ThreeJS 炫酷特效制作》

    ThreeJS 炫酷特效旋轉(zhuǎn)多面體Web頁 Demo 01《ThreeJS 炫酷特效制作》

    本案例為一個 threejs 的特效網(wǎng)頁,大小球體進(jìn)行包裹,外球體為透明材質(zhì),但是進(jìn)行了線框渲染,使其能夠通過外球踢查看其內(nèi)球體。 注:案例參考源于互聯(lián)網(wǎng),在此做代碼解釋,侵刪 本案例除 ThreeJS 外不適用任何第三方框架,放心食用 懶的同學(xué)可以直接下載代碼,打賞作

    2024年02月08日
    瀏覽(21)
  • 【Unity】常見對象旋轉(zhuǎn)方法

    【Unity】常見對象旋轉(zhuǎn)方法

    ????????記錄下常見的游戲?qū)ο笮D(zhuǎn)API。 ????????自身旋轉(zhuǎn)。 public void Rotate(Vector3 eulers, Space relativeTo = Space.Self); eulers:旋轉(zhuǎn)角度。 relativeTo:坐標(biāo)軸。 代碼: 演示: 代碼: 演示: ????????繞某一點的某軸旋轉(zhuǎn)。 public void RotateAround(Vector3 point, Vector3 axis, float angl

    2024年01月18日
    瀏覽(11)
  • Unity物體旋轉(zhuǎn)

    在Unity中,旋轉(zhuǎn)是指將對象繞其自身的某個軸旋轉(zhuǎn)一定角度的操作。 Unity提供了多種旋轉(zhuǎn)方法,以下是其中的一些: 如果需要緩慢的旋轉(zhuǎn)可以使用第4,5兩個方法之一 Unity官方還提供很多方法,可以參考官方文檔 1.Transform.Rotate方法 Transform.Rotate方法是最基本的旋轉(zhuǎn)方法,它可

    2024年02月09日
    瀏覽(28)
  • Unity 物體旋轉(zhuǎn)

    Unity 物體旋轉(zhuǎn)

    ? ? ? 在Unity中經(jīng)常會用到物體的旋轉(zhuǎn),常用的方式一般是使用歐拉角和四元數(shù)。 歐拉角: Demo: ? ?讓物體分別繞x,y,z軸旋轉(zhuǎn) 1 rad。 ? ?這里有個問題,當(dāng)物體繞x軸旋轉(zhuǎn)90度之后,再讓y或z軸繼續(xù)旋轉(zhuǎn),會發(fā)現(xiàn),物體只能繞 ? y軸旋轉(zhuǎn)。出現(xiàn)這種現(xiàn)象的原因是死鎖了。歐拉角

    2024年02月13日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包