当前位置:网站首页>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 !
边栏推荐
猜你喜欢
Electronic pet dog - what is the internal structure?
超标量处理器设计 姚永斌 第5章 指令集体系 摘录
超标量处理器设计 姚永斌 第6章 指令解码 摘录
Make a grenade with 3DMAX
一直以为做报表只能用EXCEL和PPT,直到我看到了这套模板(附模板)
补能的争议路线:快充会走向大一统吗?
Open source PostgreSQL extension age for graph database was announced as the top-level project of Apache Software Foundation
[HCIA continuous update] WAN technology
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
蓝桥:合根植物
随机推荐
中断的顶半部和底半部介绍以及实现方式(tasklet 和 工作队列)
MVC mode and three-tier architecture
gatling 之性能测试
RecastNavigation 之 Recast
ISO27001 certification process and 2022 subsidy policy summary
[HCIA continuous update] WAN technology
大规模服务异常日志检索
ARTS_20220628
Cann operator: using iterators to efficiently realize tensor data cutting and blocking processing
Win32 API 访问路由的加密网页
你应该懂些CI/CD
android使用SQLiteOpenHelper闪退
[HCIA continuous update] network management and operation and maintenance
ARTS_ twenty million two hundred and twenty thousand six hundred and twenty-eight
Interpretation of data security governance capability evaluation framework 2.0, the fourth batch of DSG evaluation collection
The Block:USDD增长势头强劲
Tutorial on the use of Huawei cloud modelarts (with detailed illustrations)
要上市的威马,依然给不了百度信心
如何提高开发质量
78岁华科教授冲击IPO,丰年资本有望斩获数十倍回报