Unity trigger detect if player is completely out of collider - unity3d

I use OnTriggerEnter and OnTriggerExit to simulate ladders. When i climb up a ladder the OnTriggerExit is fireing as soon as the top of the player collider is outside of the ladder collider while the bootom of the plyer collider is still inside of it. The same when climbing down, the OnTriggerExit fires as soon as the bottom of the player collider is out of the ladder collider while the rest is still inside. Can i (and how) detect if the colliders bottom or top is completely out of the ladder collider and not only starts to get out?

We would need more info like some code or at the very least some screenshots of your setup,
However i think you can tackle this 2 ways:
Instead of seperate triggers for each body part, instead create one big one.
Or you could chain your triggers together. In this case each trigger should call the parent component, and increment an integer (int TotalAmountOfCollidersTriggered = 0) in onTriggerExit you decrease this value. Then when the value is positive, you know one of your colliders triggers something, and when the value is 0 you know that no collider is triggered anywhere.

Related

Collider2D not detecting collisions at all unity

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.

Is there a way for a gameobject's collider to only respond to a pinch gesture?

I have a collider that when grabbed (pinch gesture), it runs a method. However, I don't want the collider react to select events. Right now, the collider prevents buttons behind it being pressed. So when the pointer is coming from the hand, the ray stops at the collider and doesn't reach to the button behind it.
From my understanding, if there is a collider, the hand interaction ray stops at it, no matter what components it has. My guess is that I need to have a layer that ignores these colliders that still allows for a grab event to be registered. But when I added a layer for the Pointing Raycast Layer Mask, it correctly ignores the collider for the far interactions, but fails to recognize a grab event.
This is how my interactable component looks:Interactable Component
I appreciate any help! Thank you
In the input settings, change the size of Pointing Raycast Layer Masks. Set UI to be element 0 so that pointing events react to that first. documentation

In Unity 2D, how do I achieve Non-trigger collsion that doesn't "physically" move the rigidbodies?

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

Unity OnTriggerEnter triggering wrong

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.

Raycast passing through box collider when trigger is enabled for object

I'm setting up footstep sounds in a 3rd person game. All works fine, except getting water collision to work correctly. I setup a simple box collider where the top of the box is on the surface of the water and the bottom of the box is below the terrain and water. However, when I set the collider to a trigger the player passes through the collider (correct) but the raycast also skips the collider and hits the next object which is the terrain (raycast is checked on in settings to collide with triggers..). of course if i turn off the trigger the player walks on the box collider, which is not what I want. The transfer.position is the in chest of the player model. the water comes up to the model's waist. Any help is appreciated. Thanks.
void Update () {
RaycastHit hit;
if (Physics.Raycast(transform.position, Vector3.down, out hit))
{
Debug.Log(hit.collider.tag);
}
}
Ideas to try:
1) Check Edit->Project Settings->Physics, make sure Raycasts Hit Triggers is checked (looks like you already did, thumbs up).
2) Make sure that your trigger is not on the Ignore Raycast layer.
3) Make your trigger thicker/wider.
4) Give your Raycast call an explicit layer mask and make sure your trigger is on that layer.
5) Make sure your trigger component is actually enabled when the call goes through.
6) Try the other *cast methods... Collider.Raycast, Physics.Spherecast, etc.
7) Solve it a different way, like using another Collider specifically for this purpose on your character object, and using OnTriggerEnter or collision events.
8) I vaguely recall having a similar problem a long time ago, and the solution involved putting a dummy RigidBody on the trigger object... something about Triggers getting filtered out in certain cases when no RigidBody is attached.
Hope that helps!