当前位置:网站首页>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 !
边栏推荐
- 【Hot100】32. Longest valid bracket
- 【HCIA持续更新】网络管理与运维
- Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
- Detectron2 installation method
- Unity 制作旋转门 推拉门 柜门 抽屉 点击自动开门效果 开关门自动播放音效 (附带编辑器扩展代码)
- Pytoch deep learning environment construction
- Rainfall warning broadcast automatic data platform bwii broadcast warning monitor
- 7 RSA Cryptosystem
- TCP两次挥手,你见过吗?那四次握手呢?
- Superscalar processor design yaoyongbin Chapter 7 register rename excerpt
猜你喜欢
爬虫初级学习
如何提高开发质量
The block:usdd has strong growth momentum
股价大跌、市值缩水,奈雪推出虚拟股票,深陷擦边球争议
机器学习概念漂移检测方法(Aporia)
【Hot100】32. 最长有效括号
Why are some online concerts always weird?
上市公司改名,科学还是玄学?
Just today, four experts from HSBC gathered to discuss the problems of bank core system transformation, migration and reconstruction
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
随机推荐
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
TCP两次挥手,你见过吗?那四次握手呢?
Win32 API 访问路由的加密网页
国产数据库TiDB初体验:简单易用,快速上手
华为云ModelArts的使用教程(附详细图解)
【系统盘转回U盘】记录系统盘转回U盘的操作
Redis主从复制
俄罗斯 Arenadata 发布基于PostgreSQL的产品
Initial experience of domestic database tidb: simple and easy to use, quick to start
Cocoscreator event dispatch use
我写了一份初学者的学习实践教程!
正则表达式
Five thousand words to clarify team self-organization construction | Liga wonderful talk
Set the transparent hidden taskbar and full screen display of the form
内核中时间相关的知识介绍
Analysis of I2C adapter driver of s5pv210 chip (i2c-s3c2410. C)
爬虫初级学习
蓝桥:合根植物
你应该懂些CI/CD
【HCIA持续更新】WLAN工作流程概述