当前位置:网站首页>Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
Unity makes revolving door, sliding door, cabinet door drawer, click the effect of automatic door opening and closing, and automatically play the sound effect (with editor extension code)
2022-07-04 18:14:00 【Tang Zhe】
Simple door making
For a novice , This tool is the best choice
The last article about opening and closing doors is relatively complex , Interested can Check the previous article, door opening and closing production
advantage
- Mount and use
- The control panel can be understood at a glance ( It's all in Chinese )
- Simple debugging can get the effect you want
- Easy to understand and modify code
panel
Parameters
- lock : You cannot operate the door after checking
- voice : Play automatically when activated
- Activate : Test the door opening and closing
- View the result location : Press and hold to view the results , Release return
The use of revolving doors is not introduced
Sliding door ( Push-pull drawer ):
- The starting position : The initial position of the door
- End position : Where the door is to be moved
- Get the position button : When you adjust the position of the door in the scene , Assign the position of the current door to the left
Code
It will be automatically added after mounting Audio Source Components
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(" Activate test ")]
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 extension code
We need to pay attention to ! This code must be placed in Editor File can be used normally ,Unity I'll take care of it myself Editor Folder , It's like Resources The folders are the same
The general camera controller I wrote before is also used in the same way
Interested can Check the general camera controller
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(" Door type ", " The type parameters of different doors are also changed ");
GUIContent[] doorOptions;
doorOptions = new[] {
new GUIContent(" revolving door "), new GUIContent(" Sliding door ") };
myScript.doorMovementType = (Door_Interaction.doorType)EditorGUILayout.Popup(doorGUIContent, (int)myScript.doorMovementType, doorOptions);
EditorGUILayout.PropertyField(open,new GUIContent(" current state "));
EditorGUILayout.PropertyField(locked, new GUIContent(" lock "));
EditorGUILayout.PropertyField(speed, new GUIContent(" Execution speed "));
EditorGUILayout.PropertyField(startClip, new GUIContent(" Sound when opening "));
EditorGUILayout.PropertyField(endClip, new GUIContent(" Sound when closing "));
EditorGUILayout.PropertyField(activate, new GUIContent(" Activate "));
GUILayout.Space(20);// interval
if (myScript.doorMovementType == Door_Interaction.doorType.RotatingDoor) {
EditorGUILayout.PropertyField(rotateAngle, new GUIContent(" Rotation Angle "));
EditorGUILayout.PropertyField(rotateAxis, new GUIContent(" Axis ", " Rotation axis of door . The default is Y "));
GUILayout.Space(20);// interval
if (GUILayout.RepeatButton(new GUIContent(" Check the end position ", " View the end position in the view ")))
{
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; // Font alignment : Level left , Vertical center
defaultStyle.normal.textColor = Color.yellow; // The font color : yellow
defaultStyle.fontSize = 15; // font size : 20
GUILayout.Label(" The starting position ", defaultStyle);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(startingPosition, new GUIContent(""));
if (GUILayout.Button(new GUIContent(" Get the position ", " Assign the current local coordinates to the start position ")))
{
myScript.startingPosition = myScript.transform.localPosition;
}
EditorGUILayout.EndHorizontal();
GUILayout.Label(" End position ", defaultStyle);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(endingPosition, new GUIContent(""));
if (GUILayout.Button(new GUIContent(" Get the position ", " Assign the current local coordinates to the end position ")))
{
myScript.endingPosition = myScript.transform.localPosition;
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(20);// interval
if (GUILayout.RepeatButton(new GUIContent(" Check the end position ", " View the end position in the view ")))
{
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();// seeing the name of a thing one thinks of its function Apply modified attributes
if (GUI.changed == true) {
EditorUtility.SetDirty(target);// This function tells the engine , The related object belongs to Prefab Changes have taken place .
}
}
}
Don't Demo 了 , Assign and paste the code according to the process, and you can use !
边栏推荐
- 【每日一题】871. 最低加油次数
- Detectron2 installation method
- 【HCIA持续更新】WLAN概述与基本概念
- 中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
- Pytoch deep learning environment construction
- Cocoscreator event dispatch use
- Wuzhicms code audit
- CocosCreator事件派發使用
- About the pit of firewall opening 8848 when Nacos is started
- ISO27001 certification process and 2022 subsidy policy summary
猜你喜欢
简单易用的地图可视化
解决el-input输入框.number数字输入问题,去掉type=“number“后面箭头问题也可以用这种方法代替
Stars open stores, return, return, return
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
曾经的“彩电大王”,退市前卖猪肉
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
如何进行MDM的产品测试
How to test MDM products
"In Vietnam, money is like lying on the street"
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
随机推荐
华为云ModelArts的使用教程(附详细图解)
DB-Engines 2022年7月数据库排行榜:Microsoft SQL Server 大涨,Oracle 大跌
无心剑中译伊丽莎白·毕肖普《一门技艺》
What if Kaili can't input Chinese???
Easy to use map visualization
Vscode modification indentation failed, indent four spaces as soon as it is saved
Load test practice of pingcode performance test
Introduction of time related knowledge in kernel
你应该懂些CI/CD
【华为HCIA持续更新】SDN与FVC
Achieve animation effect through event binding
大规模服务异常日志检索
爬虫初级学习
Five thousand words to clarify team self-organization construction | Liga wonderful talk
正则表达式
"In Vietnam, money is like lying on the street"
Redis主从复制
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
大厂面试总结大全二
RecastNavigation 之 Recast