- Home /
Adding multiple simple mesh colliders to be able to click on an objects parent
Problem
I'm making a god mode type game where you can click on buildings. Currently I'm using box colliders that take up the entire tile that the object sits on, but this results in a lot of misclicks or having to rotate the camera because another box collider is blocking it, despite it being visible.
What I'm doing now
For my first building, I've made a group in Maya called colliders and added two meshes inside of it. These are basically cubes with the edges moved around to match the roof angles.
Now I'm trying to apply this in Unity, but if I put it on the root of the prefab for the building the collider doesnt show up (the green wireframe) and in game I cannot click on it.
If I however put it on the collider object in the hierarchy, then it does show up.
My issue with this is then my check for clicking on the object doesn't work because I'm looking for the clicked object to have a specific script so I can know what type of object was clicked and update the UI. When I was using box colliders, it was on the same gameobject that had my "House" script, which is the top level of the prefab.
void Update()
{
if (Input.GetMouseButtonUp(0))
{
if (!EventSystem.current.IsPointerOverGameObject())
{
HideAll();
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
House h = hit.transform.gameObject.GetComponent<House>();
if (h)
{
ShowHouseInfo(h);
return;
}
}
}
}
}
I could get the parent object and then check for the script but I feel this isn't a great solution because then its dependent on the hierarchy of my model and I don't think code should depend on that.
Question
My question is what would be the proper way of adding a couple basic mesh colliders and then getting what "Prefab" was clicked on? Also, is it better to keep these meshes separate so they are not convex, or combine them together and use a convex collider and should they be in the same FBX file as the model itself?
Your answer
Follow this Question
Related Questions
There is somthing like mesh collider to 2D objects? 2 Answers
Clickable Object Problem - Help 1 Answer
Very Confused About Convex Mesh Collider Result 0 Answers
Collider for rocks. 1 Answer
Mesh collider and jitter 1 Answer