I am trying to work through an issue with selecting objects in my scene. The object selection script works fine for all units/resources etc as below:
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit, 100.0f))
{
if (hit.collider.transform.tag == "Terrain")
{
target = null;
} else
{
target = hit.collider.gameObject;
}
}
The issue I am having is the home building has two colliders - one for collisions (capsule) and another added via script (sphere) which is the detection range (which changes through the game). Currently the code for the detection trigger is:
Putting the collider on layer 2 puts it on the ignore raycast layer. The issue with this is it sets the whole gameobject to that layer rather than just the collider. I have been unable to find anything to indicate it is possible to have multiple colliders on one game object with different layers.
The only workarounds I can think of are:
Leave the detection layer alone but separate âignoredâ colliders using tags and raycastall - Not sure how much of an extra performance hit this would be? Also I did attempt it initially but resulted in a spectacular crash⌠(No doubt my error but I couldnt work it out at the time)
Put the detection zone collider on a new empty game object and make it a child of the home town (will mean re working the scripts a bit but probably a faster solution than 1?).
Thoughts/Advice? (And/or workarounds for 2 colliders on one gameobject??)
You shouldnât have 2 collider components on a single gameobject. Iâm surprised Unity is letting that happen without errors. Try adding a 2nd collider in the editor and see what happens.
Easiest solution is just to have a child gameobject with 2nd collider (as this is allowed see compound colliders in ridigbody section of unity manual, link below), then you can also have each object on different layers. Iâd also recommend explicitly specifying layer masks in your raycasts.
You can definitely add a 2nd collider in the editor, as long as it is not the same type (You get a dialogue box informing you there is already a collider and do you want to replace it or add the new one). I would be interested in knowing for sure you shouldnt have two colliders on one gameObject as the vast majority of tutorial resources out there I have seen use it (One as a collider for collisions and one as a trigger for detection). That is how I have handled all of my AI stuff previously (Capsule collider for collisions, sphere collider trigger for detection radius)
My current project is http://forum.unity3d.com/threads/190189-Ravage-(WIP)-Influence-based-RTS
I am rewriting how the AI and towns in particular handle stuff so it will be easy to use the 2nd workaround I mentioned in the original post (and as you pointed out - child gameobject). This will have to extend to the individual AI entities as well I guess.
Still⌠if it IS bad practice there is a lot of misconception out there (at least that I have found!)
You should never have more than one collider on a gameobject unless youâre manually enabling/disabling them at runtime via a script, at least thatâs the only sane case I can see for wanting that, even then Iâd favor adding and removing colliders at runtime over that. The warning the editor throws up is there for a very good reason, they wouldnât put the warning there if it was considered âgood practiceâ to do it.
I donât know what tutorials youâve been following that encourage doing this, but theyâre badly written if they do. There really shouldnât be a lot of misinformation about it.
Adding multiple colliders to a single object that each serve a separate concern (one for collision, one for AI triggers) just seems like a bad idea to me. You canât have 2 sphere colliders nor can you assign different layers per component which is often a key to performance tuning with the physics interaction matrix so best practice would obviously be to only ever have a single collider per object to reduce the risk of complication or conflicts with such restrictions later in development.
There really shouldnât be a lot of misconception about this, itâs not difficult to foresee the potential problems here (one of which you ran into) and thus simply have multiple objects for all of your colliders.
Interesting⌠The dialogue box that appears in Unity does not resemble a warning (to me at least - more of a âJust in case you didnt realiseâ) so I had never thought twice about it. Whilst I can now understand there is definitely more flexibility using the child gameObject method I am surprised that it is SUCH a bad idea⌠I have made small mini games (just 5 minutes of fun to practice coding) using that method and never had any issues (but I never needed performance tuning either)
I disagree that it isnt difficult to forsee problems though - maybe when you have had a good amount of experience but as a beginner I had no idea that it was such a no-no⌠Seemed simple and worked well so I assumed it was good to go. Out of curiosity for myself (and any other less experienced Unity dabblers that may read this) what are some of the other potential problems over having a collider and trigger on one object?
If it wasnât a potential issue they wouldnât throw up that dialogue box to ask to remove the old one.
Other issues? I dunno, having support for layers and possibly having to rejig a bunch of prefabs down the line really should be enough to make anyone avoid doing that. A lot of scripts/libraries/developers use the gameobject.collider shortcut when trying to access a collider of an object, and they generally assume thereâs only one so lots of potential problems there too.
Honestly, until this thread Iâd never thought anyone would want to use multiple colliders on a single object.
Last time I saw that popup dialog, it in no way deprecated the use of multiple collider components on a single GameObject - if that was the intent, then it would just warn you that the new collider would replace the old one, not offer to keep both. That said, it seems to me they are moving away from supporting that (or a least mentioning it). Iâm pretty sure one of the original tutorial videos on the Unity site showed multiple colliders on a single GameObject but that is gone and the new Learn video (and the Rigidbody documentation) only talks about making compound colliders with GameObject hierarchies. Since compound colliders are treated as single colliders, however (when used with rigidbodies), if you want to use separate layers or a mix of colliders and triggers, I think itâs better to keep those entirely separate (Iâve run into to issues where I had different PhysicMaterials within a compound collider and the collisions werenât picking up the one I expected).
In the Unity Stealth project tutorial you can see an example of having two colliders on one enemy game object. One is a capsule collider for normal collisions and one is a larger sphere collider set as a trigger used to detect if the player is in sight. Checkout the enemy setup video of the tutorial here:
I think its safe to say that lot of beginners like me would assume you can use colliders in this way. I certainly did, and it seems as though itâs fine according to the tutorial. Also, Iâm not getting a warning popup when adding multiple colliders. Maybe itâs because this thread was started several months ago and something has changed in a new version?
With that said, I did run into a problem because of having two colliders attached unnecessarily to an enemy game object. I have an enemy that will collect gems to heal itself when its life goes below a certain threshold. I couldnât figure out why the gem was giving the enemy two hit points instead of one hit point like I had specified.
I have a sphere trigger collider on the gem. The script for healing the enemy is attached to the enemy game object with an OnCollisionEnter function. The enemy has a normal capsule collider, and when I wrote the script I added another capsule collider set as trigger thinking I needed it. I discovered that the extra capsule collider set as a trigger was the cause of the OnCollisionEnter function being called twice when the enemy walked over a gem. Once I removed that extra collider, the problem was solved.
I often use multiple colliders on a game object, the Unity editor doesnât complain (why should it?) and it allows me to have finer control over my collision detection logic.
You absolutely can have multiple colliders on one GameObject AS LONG AS they are primitive colliders. Its frequently the case where you use multiple primitive colliders to replace a (slower) mesh collider. You can only have one mesh collider on an GameObject.
Now this doesnât answer the OPâs question though
Option #2, make a child object with the other layerâs collider(s). Thats the option that I use.
There is absolutely nothing strange in putting 2 or more colliders in a single game object, otherwise how would you solve complex collision shapes? Meshes are only good for convex shapes, soâŚ
Youâd solve it exactly as they describe. That being said you need to keep in mind that this thread is old now and in the time between their post and yours PhysX has received at least one update and the game engine is considerably different. What is valid now may not have been valid at all back then.
Mhhh sorry but I have to correct this. I can recall when I first met Unity, it was still V.4, and in one tutorial I remember images that showed how the colliders of a single weapon were all stored under a single game object. I am talking about primitives colliders here, as it doesnât seem Unityâs programmers want people to put more than one mesh collider in a single game object⌠as they donât allow for easy manipulation of it⌠but you can add them nonetheless, but you will have to define scale and position relative to origin already perfectly⌠a mess, in other words XD better use children objects