I am making a 2 player fighting game in unity and am using boxcollider2Ds for attack hitboxes and compositecollider2Ds for defending hitboxes. One time I noticed that the tag assignment for enemy was actually in reverse, so I changed that. but after tweaking a few more things, I noticed that the collision wasn't actually working, so I decided to print out two bools indicating if either condition was met, with the first being if the colliders were touching, and the second being if the move that was hitting you had already hit you. Both were continuously false, even after I did a move, so I soon realized that one of my colliders was in a child object but the class instance was taken from the collider in the parent object. but the collisions still didn't work and the actual collisions weren't working. Then I went to check on the box colliders and composite colliders in the actual unity UI(both being triggers) but it wouldn't detect anything, and it wouldn't even show up in the contacts tab.
for reference here are what colliders are being used
player1
boxcollider2d not a trigger
compositecollider2d: istrigger
defending hitboxes: boxcollider2d, usedbycomposite
defending hitbox 1,2,3 and 4 boxcollider2d, usedbycomposite
attacking hitbox: boxcollider2d, istrigger
can someone let me know what could be preventing it from detecting contacts?
The problem is most likely the Is Trigger bool. When it is enabled, Other objects will be able to pass through it as if it does not have a collider.
Their is also different functions for both collision and trigger. the OnCollision functions are used on non Is trigger colliders while if that bool is true, the function is OnTrigger.
Related
I made two game objects, player and enemy.
I hope two game objects collision detect and they are pass through each other.
Thus i put a check mark objects collider's 'is Trigger' and objects's 'rigid body'. So, i expected that objects are pass through each other.
But, player and enemy crashed each other. Why player and enemy crash?
i want two objects pass through each other.
help me please.
A collider marked as a trigger should not cause physical collisions with an incoming Rigidbody (see here). Check and see if one of the player or any objects has a child with a collider that is not marked as a trigger.
Also note that a CharacterController component will be constrained by collisions, as mentioned in the documentation. CharacterControllers do not have a trigger option, so you will have to disable collisions through a different method. The most common methods are using Physics.IgnoreCollision or changing the layers of the objects and disabling collisions between layers in Edit -> Project Settings -> Physics. See here for more details: Layer-based collision detection and How to Ignore Collision between Objects.
I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine
I have a game in which I want my player to jump from one platform to the other. On my platform I have a box collider.
When I tried to jump, sometimes my player would hit the side of the collider and start grinding up against it and would slip down and finally trigger the death condition. To prevent this, I put a trigger collider next to my platform. I also put the trigger collider slightly below the box collider so that in case my player jumps on the edge, he doesn't activate both.
If the trigger activates, I play the death function, otherwise I assume the player has made a solid jump and go on with the other game logic.
However, I notice that sometimes when the player lands on the edge of the platform it enters the on collision function as well as the on trigger event.
This is strange because I notice in my scene view that the player collider has not entered the trigger collider below the platform.
I have tried adding raycast to the mix but I'm still getting this bug, and even despite the raycasts I don't understand why OnTriggerEnter is triggering when the player hasn't entered the trigger.
I did consider the fact that the player collider might push into the platform collider since the player is falling with some speed but I thought it would show up in the scene view.
My question is, when I land on the platform collider how is it possible for the trigger collider to trigger? I'm jumping on top of it, not from below it. If I miss the platform completely the trigger fires as expected but on occasions it will land on top of the platform have no overlap with the trigger collider and still fire it.
/e
this is a side on view of my 3d environment. It is not the exact depiction but to simplify it for you guys. My player is jumping from one platform to the other, both of these platforms are for all means and purposes 3d cubes with box colliders. In between the two platforms but at a slightly lower level I have a trigger collider.
when my player hits the trigger collider its game over. my question is sometimes my player will land on the edge of the box collider as in the sketch but for some reason it will trigger the gameover trigger collider when there is no penetration of my player collider with the trigger collider, as you can see there is plenty of space in between them they arent even touching.
To sum up - my ontriggerenter is running when there is no visible contact between my player and the trigger collider, question is why?
ignore the draw ray, as you can see, my player collider is firmly on top of the platform box collider, and yet ontriggerenter for the collider next to and below the platform is triggered and my player goes into death animation. I cant be more specific then this, its up to you guys to help me out now and explain why this is happening. my player is far away from the triggercollider they arent touching.
As always, no response from Stackoverflow.
Anyways I was able to find a solution to my problem. The solution was ugly and its not worth sharing but for anyone else stuck with collision detection and such the following things or a combination of the following might help you find a solution.
1 - You can consider changing the rigidbodies colliding between each other to continuous or continuous dynamic. This will increase performance overhead but will lead to much better collision between the rigidbodies.
2 - In settings, physics manager you can try to turn up the Default Solver Iterations. From the documentation: Solvers are small physics engine tasks which determine a number of physics interactions, such as the movements of joints or managing contact between overlapping Rigidbody components. Use Default Solver Iterations to define how many solver processes Unity runs on every physics frame. This affects the quality of the solver output and it’s advisable to change the property in case non-default Time.fixedDeltaTime is used, or the configuration is extra demanding. Typically, it’s used to reduce the jitter resulting from joints or contacts.
These or a combination of these might be something to look at.
I noticed that OnCollisionEnter2D does not trigger when two objects with Collider2D components make contact. It only triggers when one of them has a RigidBody attached to it. This seems odd to me, because the Unity editor itself says that having a static collider in place of a non moving RigidBody is much better for performance. Then why does Unity not allow two game objects with static colliders trigger collision events when they make contact?
Sure, static colliders are more performant, but as the word indicates: they are static.
Movement requires physics calculation and therefore a rigidbody (which holds information about speed etc.). This is actually stated in the docs for the normal collision here http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
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?