当前位置:网站首页>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 !
边栏推荐
- Master the use of auto analyze in data warehouse
- Easy to use map visualization
- Great Wall Securities security does not open a securities account
- The controversial line of energy replenishment: will fast charging lead to reunification?
- 华为云ModelArts的使用教程(附详细图解)
- Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
- How to test MDM products
- The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
- 超标量处理器设计 姚永斌 第5章 指令集体系 摘录
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
猜你喜欢
谷粒商城(一)
Weima, which is going to be listed, still can't give Baidu confidence
比李嘉诚还有钱的币圈大佬,刚在沙特买了楼
I wrote a learning and practice tutorial for beginners!
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
【华为HCIA持续更新】SDN与FVC
【HCIA持续更新】WLAN工作流程概述
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
MVC mode and three-tier architecture
随机推荐
What if Kaili can't input Chinese???
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报
[daily question] 556 Next bigger element III
Load test practice of pingcode performance test
The money circle boss, who is richer than Li Ka Shing, has just bought a building in Saudi Arabia
同事悄悄告诉我,飞书通知还能这样玩
[cloud native] what is the "grid" of service grid?
你应该懂些CI/CD
Large scale service exception log retrieval
[HCIA continuous update] WLAN overview and basic concepts
用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
ISO27001 certification process and 2022 subsidy policy summary
爬虫初级学习
如何提高开发质量
regular expression
ISO27001认证办理流程及2022年补贴政策汇总
明星开店,退,退,退
With the stock price plummeting and the market value shrinking, Naixue launched a virtual stock, which was deeply in dispute
蓝桥:合根植物
New technology releases a small program UNIPRO to meet customers' mobile office scenarios