Unityでタップ演出を実装する
概要
こんな感じのタップ演出の実装方法です。
パーティクルの用意
白く光る波紋はパーティクルを使っています。今回はテクスチャなどの用意はしていません。 設定は以下のようにしています。
color over lifetimeも設定します。
左上のアルファは255、右上のアルファを0に設定しています。 続いて、size over lifetimeを設定します。
早い段階で急速に大きくなりそのあとは緩やかに大きくなるようにしています。 最後にLayerの設定をします。
Emmitやshapeは今回は不要なのでチェックが入っている場合は外します。
カメラの用意
今回は、タップエフェクトだけを映す専用のカメラを用意しました。 設定です。
逆にMainCamera側にはタップ演出は映らないように設定します。(2重に見えてしまうので)
CullingMaskからTransparelentFXのチェックを外します。
スクリプトの用意
以下のスクリプトを用意して、一番はじめに呼ばれるシーンにGameObjectを用意しアタッチをしました。
using System.Collections; using System.Collections.Generic; using UnityEngine; public class EffectController : MonoBehaviour { [SerializeField] ParticleSystem tapEffect; [SerializeField] Camera camera; private static bool isTapEffect = false; void Awake() { if (!isTapEffect) { DontDestroyOnLoad(this.gameObject); DontDestroyOnLoad(tapEffect); DontDestroyOnLoad(camera); isTapEffect = true; } } void Update() { #if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR // iOS and Android if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { var pos = camera.ScreenToWorldPoint(Input.mousePosition + camera.transform.forward * 20); tapEffect.transform.position = pos; tapEffect.Emit(1); } } #else if(Input.GetMouseButtonDown(0)) { var pos = camera.ScreenToWorldPoint(Input.mousePosition + camera.transform.forward * 20); tapEffect.transform.position = pos; tapEffect.Emit(1); } #endif } }
シーンをまたいで使いたかったのでDontDestroyOnLoadをしています。
以上で実装完了です!