Владимир Шека 4 Share Posted July 12, 2018 Очень простой скрипт, который позволит сделать пианино на игровом движке Unity. Скачать скрипт и префаб пианино https://yadi.sk/d/nQsME47c3Z673H PianoKey.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class PianoKey : MonoBehaviour { // звук для воспроизведения при нажатии клавиши (нота пианино) public AudioSource sound; // ссылка на главный скрипт, чтобы брать из него инфу public PianoManger Manager; // при нажатии на клавишу void OnMouseDown() { // утапливаем ее transform.position -= Manager.pressureDegree; // активируем звук клавиши sound.Play(); } // при отпускании мыши void OnMouseUp() { // возвращаем клавишу на место transform.position += Manager.pressureDegree; } void Start () { // проверка, что вы не повесили два раза скрипт на клавишу PianoKey[] countScript = GetComponents<PianoKey>(); if (countScript.Length > 1) Debug.LogWarning("На клавише больше 1-го экземляра скрипта! " + transform.name); // проверки есть ли ссылка на аудио сурс и есть ли в аудио сурсе аудиоклип if (sound == null) Debug.LogWarning("Укажите, какой AudioSource будет для кнопки! " + transform.name); if (sound.clip == null) Debug.LogWarning("Задайте Clip в AudioSource в инспекторе! " + transform.name); } void Update () { } } PianoManger.cs using System.Collections; using System.Collections.Generic; using UnityEngine; public class PianoManger : MonoBehaviour { // степень нажатия клавиш [Tooltip("Укажите, насколько смещать клавиши при нажатии")] public Vector3 pressureDegree = new Vector3(0, 0.002f, 0); // Use this for initialization void Start () { } // Update is called once per frame void Update () { } } Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.