12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unityで球の内側にいる時に当たり判定を設定する

Posted at

No hack, no work • UnityとOculusで360度パノラマ全天周動画を見る方法【無料編】のSphere100.fbxを利用して全天球画像を表示しているがRayキャスティングを実施しても反応がなかったので対応した。
ここでは対応方法をメモする。

参考サイト

原因

Colliderが外向きからしか反応しない。
内側からだと当たり判定が存在しない。

対応方法

  • Sphere100にMesh Colliderコンポーネントを追加する
  • Sphere100に以下のスクリプトを設定する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ReverseNormals2 : MonoBehaviour {

    // Use this for initialization
    void Start()
    {
        MeshFilter filter = GetComponent(typeof(MeshFilter)) as MeshFilter;
        if (filter != null)
        {
            Mesh mesh = CopyMesh(filter.mesh);

            Vector3[] normals = mesh.normals;
            for (int i = 0; i < normals.Length; i++)
                normals[i] = -normals[i];
            mesh.normals = normals;

            for (int m = 0; m < mesh.subMeshCount; m++)
            {
                int[] triangles = mesh.GetTriangles(m);
                for (int i = 0; i < triangles.Length; i += 3)
                {
                    int temp = triangles[i + 0];
                    triangles[i + 0] = triangles[i + 1];
                    triangles[i + 1] = temp;
                }
                mesh.SetTriangles(triangles, m);
            }
        }

        this.GetComponent<MeshCollider>().sharedMesh = filter.mesh;
    }

    // Update is called once per frame
    void Update () {
        
    }

    public Mesh CopyMesh(Mesh mesh)
    {
        var copy = new Mesh();
        foreach (var property in typeof(Mesh).GetProperties())
        {
            if (property.GetSetMethod() != null && property.GetGetMethod() != null)
                {
                property.SetValue(copy, property.GetValue(mesh, null), null);
            }
        }
        return copy;
    }
}
12
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?