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

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.

Related

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

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.

Prevent pushing while still detecting collisions in Unity

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.

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

How to use CharacterController and BoxCollider simultaneously?

Here is the problem, I have a troop controlled by CharacterController and I want the troop to block if collide with something. So I add a box collider to it. But it's not working, as the figure shows. The cube doesn't block the BoxCollider but does block the CapsuleCollider in CharacterController.
Short answer: It just doesn't work.
Character Controller is always using its own CapsuleCollider and there is nothing you can do about it. It's a feature requested for several years, but there doesn't seem to be any interest to add this feature in the near future.
You need to replace the CharacterController with a Rigidbody and write your own controller (or copy&paste one) to handle movement. Basically you use Rigidbody.AddForce to move your player.
If you need some ideas how to implement it, have a look at these tutorials. They all use a different approach to control movement:
http://unity3d.com/learn/tutorials/projects/roll-a-ball/moving-the-player
http://unity3d.com/earn/tutorials/projects/space-shooter/moving-the-player
http://unity3d.com/learn/tutorials/projects/stealth/player-movement