0

I am making a game with Unity, the project model is 2D.

What I have to do is a wheel divided into segments, each segment is an individual object, this wheel turns on itself with a certain speed, here is an imamgine for better understanding: enter image description here

I have a "Selector", i.e. something to select a slice, so I made a temporary sprite, i.e. the red triangle, and a script to generate a Ray Cast to locate the selected slice, some images to better understand:

enter image description here

So far so good, my problem lies in the fact that the mesh of the slices is also the mesh of the collider, which is not convex but concave, so from what I've read on the Internet Unity does not allow to intercept objects with concave mesh by the Ray Cast due to calculation problems, so I can not intercept the slices, the only way to hit them is to tick the parameter "Convex" of the Collider component, but I create a collider with a square shape, and so the selection precision is lacking, here are some pictures to better understand:

enter image description here

So I looked on the internet for a solution and found that the solution was to split the collider into several smaller but convex colliders, so I tried this, i.e. for each pair, i.e. 2 triangles, I created a collider that had a mesh made from the two triangles in question, so I got this:

enter image description here

But it is still not intercepted by RayCast, unless I tick the "Convex" parameter of the collider component, but even then a collider with a square shape is created.

enter image description here

Finally, here are some parts of the code within the post:

Code for generating ray cast:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Selector : MonoBehaviour
{
    public GameObject objPosRef;
    void Update()
    {
        if (Input.GetMouseButtonUp(1))
        {
            RaycastHit hit;

            Debug.DrawRay(objPosRef.transform.position, new Vector3(0, 0.5f, 0), Color.green, 0.5f);
            if (Physics.Raycast(objPosRef.transform.position, new Vector3(0, 0.5f, 0), out hit))
            {
                Debug.Log("Colpito: " + hit.collider.name);
            }
        }
    }
}

Code to update the segment mesh:

...
private void UpdateMesh()
{
    mesh.Clear();
    mesh.vertices = vertex;

    mesh.triangles = triangles;

    createColldiers();

    mesh.RecalculateNormals();
    mesh.RecalculateBounds();

    GetComponent<MeshRenderer>().material = new Material(material);
    GetComponent<MeshRenderer>().material.color = color;
}

Code for creating segment colliders:

private void createColldiers()
    {
        int numColliders = (numVertex * 2 - 2) / 2;

        for (int i = 0; i < numColliders; i++)
        {
            MeshCollider collider = gameObject.AddComponent<MeshCollider>();
            Mesh mesh = new Mesh();

            int[] tr = new int[6];

            int k = 6 * i;
            for (int j = 0; j < 6; j++)
            {
                tr[j] = triangles[k];
                k++;
            }

            mesh.vertices = vertex;
            mesh.triangles = tr;

            collider.sharedMesh = mesh;
        }
    }

In summary I create a new mesh, the vertices of this mesh are identical to those of the segment mesh, although the new mesh does not need all the vertices, the triangles of the new mesh are only 2, and are taken in pairs from the triangle array of the segment mesh.

Sorry if my post is full of pictures, I hope I have been able to give you the best possible understanding of my problem, most likely the probelma will be something popping, thank you in advance for your help.

p.s: very few people say that raycasts can safely intercept objects with a concave collider, thus going against the grain of all other claims to the contrary, who is right?

Titan
  • 99
  • 7

0 Answers0