- Home /
What is the most efficient way to have a rigid body object ignore all collisions except with one thing?
Hello and Happy Holidays!
I have objects that need to have a rigid body to interact with 1*one*1 thing in the game but I don't want them colliding with anything else. I know about find object with tag but that seems silly and inefficient to define each and every other class of game object in that way. For example...
var bub0Ref = GameObject.FindGameObjectsWithTag("Bub0");
var bub1Ref = GameObject.FindGameObjectsWithTag("Bub1");
var bub2Ref = GameObject.FindGameObjectsWithTag("Bub2");
var bub3Ref = GameObject.FindGameObjectsWithTag("Bub3");
PLUS... in my game different GO's are instantiated at random times and having to call find objects"..." over and over again to make sure that none of them are slipping by (and causing collisions) rankles me.
I saw the collision manager on the wiki but it's done in c# and I've been strictly JavaScript. Is there a javascript version somewhere?
I've also seen other responses about using classes but they were a bit over my head.
Can I do it with a layermask? Or can one GO have multiple tags somehow to put similar if not identical things into a class of object?
Any help is appreciated!
It's Christmas day! --Goody!
Answer by jashan · Dec 26, 2009 at 10:58 AM
In my experience, in a client-server-based game where I need to disable all collisions between game groups (groups of people who may be playing in the same level but who must not interact in any way with the players of the other groups), this is not a problem performance-wise.
I have a CollisionManager that calls Physics.IgnoreCollision in both directions for each object that already exists whenever a new object is instantiated. However, I have my own book-keeping which is based on Lists: Each game group has a list of players to which the relevant game objects are attached in lists; that way I have very fine control over who can interact with whom. That fine-grained control is useful for a lot of other things, too, so I don't only have that for my collision management.
Here's an example method from my CollisionManager (I'm calling this whenever a new "wall" is created which happens whenever a player makes a turn, so that's quite frequent; especially when there's a lot of player on the server); it's in C# but should be easy enough to understand and convert to JavaScript:
public void IgnoreWallCollisions(Collider currentWallCollider) {
foreach (GameGroupData otherGameGroup in GameManager.Instance) {
if (otherGameGroup != this.gameGroup && otherGameGroup.Level.IsPlaying) { // look at all except for myself...
foreach (TeamData team in otherGameGroup) {
foreach (PlayerData player in team.TeamMembers) {
if (player.IsAlive && player.CycleParameters != null && player.CycleParameters.collider != null) {
Physics.IgnoreCollision(player.CycleParameters.collider, currentWallCollider, true);
Physics.IgnoreCollision(currentWallCollider, player.CycleParameters.collider, true);
foreach (Projectile projectile in player.Projectiles) {
if (projectile != null && projectile.collider != null) {
log.DebugFormat("Ignoring projectile-wall collisions between: {0} and {1}", projectile.gameObject.name, currentWallCollider.name);
Physics.IgnoreCollision(projectile.collider, currentWallCollider, true);
Physics.IgnoreCollision(currentWallCollider, projectile.collider, true);
}
}
}
}
}
}
}
}
You may consider voting up Physics: Layer based Ignore Collision as this might help simplify things for you a little bit.
Answer by Ashkan_gc · Dec 26, 2009 at 08:19 AM
there is not any simple way to do this in unity now. you should find all gameobjects that you want to ignore collisions with them and call the Physics.IgnoreCollision method for them. but for having a list of gameobjects you can put a reference from each game object in to a list in it's creation time. use a List. i don't know if you can use generics in current js compiler or not. sorry
Your answer
Follow this Question
Related Questions
Continuous collision detection - unnatural behaviour 0 Answers
Can I change the order that collision layers collide with each other? 1 Answer
how the ball bounces of equal in angles? 1 Answer
Do you have to have a rigidbody component for collision detection? 1 Answer
Keeping object with character controller from staying on top of another character controller object 4 Answers