Is there any way to make a collider collide only with a specific GameObject without using layers? - unity3d

Basicly I have a GameObject with BoxCollider attached to it and I want it to collide only with one specific BoxCollider, ignoring all other collisions. Is there any way to do that without using layers? I want to avoid using layers because I need that system to be as flexible as its possible.
I can technicly use Physics.IgnoreCollision but it will drastically lower game performance and I want to avoid it.

Although it is a 'trick' method, there is a way to add a specific class component only to a specific collider that you want to collide, and then, when processing it in another place, there is a way to determine the class.
Hope this helps.

Related

Rigidbody2D or raycasting?

I am working on a 2D game that involves many blocks that stack and collide on eachother and the player. I am currently using Rigidbody2D on the blocks for dynamic collisions but I am not a fan of how the dynamic physics include the "bounciness" in the elastic collisions. Also, there is an inherent push-force as well as other unfavorable push physics that are too "realistic".
I am wondering what is the best way to handle my predicament to remove the bounciness and push elements of the rigid bodies. I've tried adjusting the block's masses and bounciness physics but no luck. Is there a way to set them all as kinematic or disable these realistic effects somehow and still have them collide via rigidbodies? (Kinematic would be great if they were able to collide with each other) Or will I have to create some sort of raycast based block physics handling script? Or is there an even better solution to creating this very primitive physics structure that I am not seeing?
Thanks for any and all help!
The only way I could think of solving your problem, such that you have complete intuitive accuracy, is to write your own Rigidbody controller. Of course you can still reuse the Box colliders.
Once you've decided on a collision detection method and manifold generation working (raycasting maybe), this link details the impulse resolution you need.

Unity ignore collisions on the same layer

I want to use Physics.IgnoreCollision to avoid bullets hitting themselves.
The bullets are spawned on layer 8. Why is this not working? How can you ignore collisions with everything on the same layer?
// bulletscript.cs
gameObject.layer = 8;
// maingamescript.cs
Physics.IgnoreCollision(8,8);
There may also be other objects on layer 8 that should also be ignored.
(for example, the player ship).
Why not use the handy dandy Physics Manager. Go to Edit->Project Settings-> Physics and set up the proper layer collisions.
Go to Physics Manager and un-check the layer with itself to avoid collision.
Troubleshot:
Unity bug, try to update to latest Unity 5.3.x (5.4 currently has nasty bugs)
Make sure the bullets and players are effectively in the wanted layer
The change in layer takes some time to take effect (few frames), since bullet are fast, you are probably encurring in that lag (so in the time the change take effect the bullet already hitted the target)
The best way is to have a Bullet Prefab that is spawned already in the correct layer, you can later customize that by changin graphics at runtime if you like, but to avoid the lag it should be instantiated already in the correct layer.
There is no need to go to physics manager, the OP already do the correct code equivalent to the physics manager. The real problem is that the GameObject should be already instantiated with the correct layer, because layer update may take some time to get effective.
Another workaround is to disable/reenable the collider.

Determine on which collider the collision has taken place

I have a gameobject with two sphere colliders attached. One has IsTrigger checked and the other not.
I want to execute different set of statements when collision occurs with different colliders. For example I want to play different sound for both different collisions. Is there any way to achieve it?
I tried OnTriggerEnter() but unfortunately it is called for both type of collisions since other colliding objects have triggered colliders. I just thought if we could somehow find out on which collider of the gameobject the collision has taken place we will be able to achieve it.
So is there any way to get through with this?
I have been using Unity for years and faced tons of problems like this, related to bad software design. I hope Unity guys will handle physics more carefully in future releases.
In the mean time, you can use Physics.OverlapSphere and Physics.CheckSphere to manually check if there is something that collides with your object. Remove the collider that you are using as a trigger and use these methods instead of OnTriggerEnter. This is a bit hacky, but this will do the job I think.
Make your colliders visible in the inspector (make them public or add [SerializeField] before it) and then tie in the colliders to the code that way.
Then, in your collisions, compare the colliding objects against your variables that are holding the colliders for you to keep them separate.
To detect for source trigger in OnTriggerEnter, you must use workaround with multiple gameobject hosting trigger and satellite scripts.
Allow me to link to my answer on gamedev SO:
https://gamedev.stackexchange.com/a/185095/54452

2D physics on game object made up of components

Say you want to let your users create a 2D airship (side view) made up of components. One component could be a floating balloon, another could be a storage room, etc.
And say you want this airship to have physics applied to it as a whole, with each component playing a part in this. For example, balloons would take away from its down force and other compartments would apply down force to it.
At the same time, the whole airship works as a whole (or at least the only physical separation would be between floater components and the others, which apply weight), so physics are applied to it as if it was one body.
Now, how can this be managed? How can I customize a game object via a script, giving it different components with different weights, and have it behave like it was a single premade object?
I'm sorry if the question is simple, but I'm only getting started with unity. Thank you very much!
TL;DR: Customizable airship gameobject with different components that have different weights. How to make it behave as a single physics entity and manage its different components?
I'm not sure that I completely understood the question :D
You mean having a game object, that has some child game objects, each with individual Collider and Rigidbody, and the Rigidbody physics applied to the parent game object? You can use joint colliders for this purpose such as HingeJoint2D, and set the parent's Rigidbody as ConnectedRigidBody.
(And instead of the word "component", use child or part or sth else to prevent ambiguity. 'Cuz the components in Unity3D are stuff like scripts, rigidbody, collider, etc. which are attached to game objects.)

Can I move dynamic physics bodies using SKAction when only contact detection is needed?

I am looking at tutorial where things are defined like this:
planes are sprites with dynamic physics bodies
plane moving is done with actions by following the path
there is contact detection between bullet and plane
bullet is sprite and it has physics body set to be static (which is little unusual in my opinion)
Here is the link to tutorial for more information.
Questions:
When we use actions to move physicsBodies is there a difference how we set body's dynamic property? Because bullet is static but still there is no problem for movement.
When we have situation like this, where we don't need collision detection, but just contact detection, and we can't move sprites (enemies) by applying forces or impulses, what is a good approach? Is this approach correct?
I think this is nice way, but I would like to be fully aware what is really happening when we use this method and are there any drawbacks or possible problems.
There are posts on SO that suggest we shouldn't use actions for moving dynamic physics bodies. I am aware that we can't use this approach in every case. For example this would not work for moving platform with other object on it, because there would be no correct physics simulation between body on the platform and platform moved by action. But in cases like from this tutorial, where we only need contact detection for object that can be moved only by actions (moved along path) I suppose it's not a problem ?
static means that the body isnt affected by physics. That doesnt mean it cant be manually repositioned or moved. So if something is set to static, it participates in the physics simulation, but it isnt affected by it. Think of a plane hitting a mountain. The plane is dynamic, the mountain is just going to sit there even though its participating in the physics. But you could still move that mountain around manually using a position or an action.
Not totally understanding your question, but I'll give it a shot.
You can move physicsBody's manually (using position property or actions), but you need to ask yourself why you're doing that. You typically don't want to do it because they're bypassing the physics simulation. If you're forcing it to move around, what's the point of using a physics body in the first place.. right?
But if your physicsbody is something like a powerup that is totally static, and you just want it to move around in a circle using an action then thats fine. You probably just want to use contact detection for the bullet, powerup, etc without actually moving it around using physics. Nothing wrong with that.