Prevent pushing while still detecting collisions in Unity - unity3d

I am making a 2D game. There are 2 characters, that can shoot bullets into each other and walls. I need to detect collisions between bullets and characters, so bullets and characters Rigidbody2D type should be dynamic. I need to prevent characters from pushing each other, but i have no idea, how to do this, without changinh their Rigidbody2D types. Making all them triggers doesn't work, beacause it will make walls passable. Help me please.

The best solution and something you should start getting use to is putting different collider groups onto different layers and then setting which ones can collide with each other in the project settings panel.
Edit -> Project Settings -> Physics2D
The sort of layer setup I think you are looking for

Try making the Bullets triggers, then in a component on the Bullet object you can Destroy(gamobject) or otherwise redirect the bullet in its OnTriggerEnter2D method.
That would mean walls are impassable, and bullets don't push anything.

Related

Child Components with rigidbody

Context
I have in my project two elements. A player (just a cube) and some eyewears.
When the glasses aren't attached to the player, i want them to have properties of a Rigidbody. But when the eyewears are attached to the player, i want them to be a static object, so the player can process the collisions and physics.
I have tried:
DestroyElement(rigidbody) when the player pickup the eyewears. When he leaves them, i recreate the rigidbody with AddComponent
It worked nice, but in the future other elements will be attached and they will not share the same properties of rigidbody. I though maybe i could save the rigidbody instance, so when the player leaves the glasses i assign it to them. I could't.
AddComponent don't accept arguments.
Then i tried to set "kinematic mode" when my player wears the eyewears. It didn't go well, my player can't jump anymore and sometimes he glitches in the floor.
How can i resolve this?
GameObject.AddComponent does take an argument, or a generic argument (preferred):
go.AddComponent<RigidBody>();
this is also possible, but deprecated, since you lose type-safety:
go.AddComponent(typeof(RigidBody));
However, RigidBody is not meant to be added/removed, and in your case I would say that kinematic mode is the way to go... but I can't tell why you're experiencing weird results with it.
Thanks to R Astra i checked again the eyewear collisions and i found out the problem. I had enabled the Convex mesh.
The col was causing trouble because the back meshes were inside the player
So, quickly i copied that eyewear and generate a new mesh. It worked!
Thank you very much!

Unity, OnMouseOver blocked by another gameobject with a collider in front of it

I have two gameobjects, both with 2D colliders.
One of them can be behind the other, and because of this its OnMouseOver can be blocked from firing as the GameObject in front blocks it from triggering.
What is a way around this? I really like the ease of using OnMouseOver, and would rather not use raycastAll.
You have 3 possibilities:
Use RayCast.All() but you said, that you want to avoid that.
Set the "blocking" GameObject to the Layer "IgnoreRayCast"
That answer has a third possibility.

Make two physics objects not collide but do detect collisions in Unity

I have a Unity project in which there is a 2D game world that consists of static colliders to make the geometry solid to the characters that inhabit it. The player is a dynamic collider (with a non-kinematic rigidbody). There's also an enemy character that is also a dynamic collider. Both characters walk over the floor and bump into walls like I'd expect them to.
What I want to achieve is that the player and enemy are not solid to each other, so they can move through each other. I achieved this by putting the enemy and the player on separate layers and setting the collision matrix so that these layers do not collide with each other. The problem I'm having now, however, is that I do want to detect whether or not the enemy and the player ran into each other. I added a trigger collider to the enemy character, it's on the enemy layer which means it doesn't detect collisions with the player.
I thought of making a sub-gameobject for the enemy, put it on the player's layer, add a rigidbody and trigger collider to it and use that to detect collisions between the player and the enemy, but it feels so convoluted that it leaves me wondering if there isn't a more elegant solution for this.
Edit (2020-05-26): Nowadays you can tell the engine to ignore collisions between two given objects. Kudos to Deepscorn for the comment. Will leave the rest of the answer unchanged, and read with that in mind.
Yes, you need to create a child GameObject, with a trigger collider, and put it in a layer that interacts with the player layer.
No, you don't need to add a Rigidbody to the new GameObject, the parent's rigidbody already makes it a dynamic collider, so you will get OnTrigger events.
As a side note, just to keep things organized, if you create a child of the enemy don't put it in the player layer. For example, in the future you might need to disable the player's layer collision with itself. Furthermore, if your player interacts this way with many objects, I'd put a single trigger on the player instead of the enemies, on a separate PlayerTrigger layer, just to keep things simple.
Isn't there a simpler way? Not really. You definitely need non-interaction between the player and enemy colliders, but some kind of interaction between them too. So one of them needs to span two layers, or the whole interaction would be described by a single bool. The physics engine processes lots of information in one go, so you can set all the layers and collisions you want, but during the physics loop you have no further control on what happens. You can't tell the engine to ignore collisions between just two objects. Having only 32 layers, and having them behave in the rigid way they do are actually heavy optimizations. If you are concerned about performance for creating another layer, disable interaction between layers you don't need, like the trigger layer and the floor and walls, or layers that don't even touch.
Your alternative is doing it all by code, which is even less elegant. A single child capsule on the player doesn't sound that bad now, doesn't it?

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

Getting Skybox to change on collision in unity with UnityScript?

I'm quite new to UnityScript as my area of interest is python, however me and a friend are planning to create a small indie game and I need the skybox to change on collision in unity. I would prefer this to be done in javascript if at all possible. Please take a look at it and let me know whats wrong with it, as when run it makes no difference to the scene.
#pragma strict
var mat:Material;
function OnTriggerEnter(trigger: Collider){
RenderSettings.skybox=mat;
}
That is the entire script. Thank you for any help given
make sure that for your objects that collide... typically one must have a static collider (e.g. floor, wall) and the other a collider and rigidBody to hit it (e.g. player, car). Also check your physics settings to make sure the layers that the objects are in, can hit each other.
Make sure you have your script on the right object... collider or collidee ? try putting a script on both objects with a print in each.
Check here for more info
http://docs.unity3d.com/Documentation/Manual/Physics.html
What exactly to you mean by changing on collision? Skyboxes cannot be collided with. If you mean objects colliding other than the skybox, make sure that at least one of the collider's has "Is Trigger" checked in the inspector.