当前位置:网站首页>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 !
边栏推荐
- Device interface analysis of the adapter of I2C subsystem (I2C dev.c file analysis)
- “在越南,钱就像躺在街上”
- 2022 national CMMI certification subsidy policy | Changxu consulting
- Russia arena data releases PostgreSQL based products
- Achieve animation effect through event binding
- Detectron2 installation method
- Talk about seven ways to realize asynchronous programming
- 用于图数据库的开源 PostgreSQL 扩展 AGE被宣布为 Apache 软件基金会顶级项目
- LD_LIBRARY_PATH 环境变量设置
- [HCIA continuous update] WLAN overview and basic concepts
猜你喜欢
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
爬虫初级学习
mysql5.7安装教程图文详解
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
为啥有些线上演唱会总是怪怪的?
[unity ugui] scrollrect dynamically scales the grid size and automatically locates the middle grid
庆贺!科蓝SUNDB与中创软件完成七大产品的兼容性适配
wuzhicms代码审计
Superscalar processor design yaoyongbin Chapter 5 instruction set excerpt
Load test practice of pingcode performance test
随机推荐
【Hot100】31. 下一个排列
正则表达式
[HCIA continuous update] WAN technology
78 year old professor Huake impacts the IPO, and Fengnian capital is expected to reap dozens of times the return
curl 命令妙用
【HCIA持续更新】广域网技术
Blood spitting finishing nanny level series tutorial - play Fiddler bag grabbing tutorial (2) - first meet fiddler, let you have a rational understanding
解读数据安全治理能力评估框架2.0,第四批DSG评估征集中
Developers, MySQL column finish, help you easily from installation to entry
Win32 API 访问路由的加密网页
wuzhicms代码审计
华为云ModelArts的使用教程(附详细图解)
S5PV210芯片I2C适配器驱动分析(i2c-s3c2410.c)
你应该懂些CI/CD
DB engines database ranking in July 2022: Microsoft SQL Server rose sharply, Oracle fell sharply
MySQL常用增删改查操作(CRUD)
Solve the El input input box For number number input problem, this method can also be used to replace the problem of removing the arrow after type= "number"
Initial experience of domestic database tidb: simple and easy to use, quick to start
The top half and bottom half of the interrupt are introduced and the implementation method (tasklet and work queue)
regular expression