Closed3

タップした箇所にエフェクトを表示

0y00y0

処理の流れ

  • タップ入力を検出する
  • タップ位置をスクリーン座標からワールド座標へ変換する
  • その位置にエフェクトを生成する
  • 一定時間後にエフェクトを破棄する(または再利用)
0y00y0

カメラの種類と座標変換の違い

項目 2D 3D
カメラ Orthographic(平行投影) Perspective(透視投影)
ScreenToWorldPointのZ 無視できる(Z=0でOK) Zを明示的に指定する必要
出現位置 XYにZ=0で配置可能 Z値によって位置が変わる

奥行きと衝突判定

項目 2D 3D
奥行き(Z軸) 固定でOK 必ず指定が必要
タップ判定 Physics2D.Raycast Physics.Raycast
UIとの重なり判定 可能(EventSystem) 同様に可能

描画対象の設定方法

項目 2D 3D
エフェクト例 Sprite, Particle System Mesh, Particle System
ソート方法 Sorting Layer + Order in Layer Z位置やDepth(描画順)で調整
常に前面に出す方法 Orderを高くする カメラに近いZ座標に置く

座標の解釈

観点 2D 3D
対象空間 XY平面中心 XYZ空間
座標変換 Z=0でScreenToWorldPoint可能 カメラからの距離でZ指定が必要
0y00y0

実装例

using UnityEngine;

public class TapEffectSpawner : MonoBehaviour
{
    public GameObject tapEffectPrefab;

    void Update()
    {
        if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
        {
            Vector2 touchPosition = Input.GetTouch(0).position;
            Vector3 worldPosition = Camera.main.ScreenToWorldPoint(touchPosition);
            worldPosition.z = 0; // 2Dの場合Zを0に固定

            GameObject effect = Instantiate(tapEffectPrefab, worldPosition, Quaternion.identity);
            Destroy(effect, 1.5f); // 1.5秒後に削除
        }

#if UNITY_EDITOR
        // エディタ用:マウスクリックでもタップ判定
        if (Input.GetMouseButtonDown(0))
        {
            Vector3 clickPosition = Input.mousePosition;
            Vector3 worldPosition = Camera.main.ScreenToWorldPoint(clickPosition);
            worldPosition.z = 0;

            GameObject effect = Instantiate(tapEffectPrefab, worldPosition, Quaternion.identity);
            Destroy(effect, 1.5f);
        }
#endif
    }
}

このスクラップは4ヶ月前にクローズされました