How to make TriggerEnter working in unity? - c#

i'm trying to make character to lose hp when it hits enemy trigger, but it's not working I'm using OnTriggerEnter2D and debugging triggers name, but there's nothing in logs.
Enemy
Character
Script

Are they on the same layer? Try debugging if they collide at all and make sure the colliders trigger flag is true. Try also to put on both objects Rigidbody2D and see if it helps.

Related

How to make Trigger enter working in unity?

i'm trying to make character to lose 1 hp when it hits enemys trigger, but it's not working
I'm using OnTriggerEnter2D and debugging triggers name, but there's nothing in logs.
Enemy
Character
Character Controller script
Add a Rigidbody2D component to your Enemy and set it to IsKinematic. Also, it doesn't look like your Character has a collider attached to it so add a BoxCollider2D to the Character, too (unless it has one, I can't see all of its components).
Objects that have trigger colliders attached also need a rigidbody component even if they aren't physics objects. Making the rigidbody kinematic will ignore the physics system but allow the collision to be detected.

Use IsTouching() in a trigger collider

I have this code for destroying an enemy when player touches it:
It works when the collider has isTrigger set to false. I've read the documentation and I think it's because it uses the physics engine to check whether player is touching or not.
Is there an easy way to avoid this, I like the cleanliness of isTouching and I need it to have a trigger collider so it doesn't affect the player movement.
If there isn't, is there a way to use the collider I already have to check for the collision with player?
I did it with OnTriggerEnter2D as jmalenfant suggested.

How to make my button change on collision

Hey I am trying to make my sprite button go from green to red and backwards when my player collides with it. I have a boxcollider2d as is trigger, and a script
script : https://gyazo.com/ec64a2bca5b23526c6949bf18cb50a0d
I think this is some of it but I dont get anything at all when colliding with the button can anyone enlighten me on this problem would be very appreciated.
Thanks in advance
It is important to distinguish the difference between Colliders and Triggers.
Colliders are used by the Physics system in order to make it so object do not penetrate or overlap each other. Triggers on the other hand are made so that you can check for overlapping areas, an example is a Ring in Sonic. Sonic collects the rings but they do not stop him from passing, think of these as triggers. If they were colliders sonic would jump into them and bounce off.
In Unity the process for creating a trigger is the same for creating a collider, with 1 additional step, and that is marking a check box.
As such it is also important to know which functions you should use in each case.
OnCollision... For Colliders, (Or Colliders not marked as triggers)
OnTrigger... for Triggers, (Marking a collider in unity as a trigger makes it a trigger.)
To just add a bit more detail, It is also important to use the correct collider types for the correct dimensions of the game, 2d Colliders/Triggers do not interact with 3d Colliders/Triggers. More so physics components: 2d Rigidbodies do not works with 3d colliders, and 3d rigidbodies do not work with 2d colliders/triggers.
As such there are functions for each 2d, 3d, collision, and trigger.
Another important factor is to ensure if you want to use these functions atleast one of the objects have to have a rigidbody of the correct type(2d or 3d).
As such the problem you were running into was using OnCollisionEnter2D for a "collider" that was marked as a trigger, in this case OnCollisionEnter2D is not called, but OnTriggerEnter2D is called.

Detect a kinematic rigidbody collision with a static rigidbody using Collider2D.IsTouchingLayers

I have a tilemap called Moving Platform and attached to it is a kinematic rigidbody (with layer mask as "Ground"). Further, there is a foreground tilemap with a layermask "Ground" (and static rigidbody) assigned to it. The scene arrangement is shown in the screenshot below:
Components attached to the platform are shown below:
Components attached to the foreground are shown below:
The highlighted platform moves to right and collides with the foreground. I am trying to detect this collision in my Update() function, but the debug log always says false
Debug.Log(myCollider.IsTouchingLayers(LayerMask.NameToLayer("Ground")));
How do I detect the collision between the two tilemaps?
If I replace the "Ground" by "Player" the collision is getting detected, since player has dynamic rigidbody attached to it. It doesn't work even if I make the platform collider a trigger collider or made the platform as dynamic rigidbody. OnCollisionEnter2D isn't working either.
This is a few years old but I found myself having the exact same problem. The fix I found is to check the box labelled "Use Full Kinematic Collisions" on the Kinematic Rigidbody. This will allow Kinematic/Kinematic and Kinematic/Static collisions, which is what we're looking for!

c# OnTriggerStay with Collider Variables

So I'm making a simple "puzzle" using lights. Simply, there are 3 buttons with lights on them (red, blue, green). Each button has it's own trigger volume but when I go to play, nothing prints that I even enter, stay, or leave the trigger. I've never used Collider Variables before so I feel like I'm doing something wrong (obviously or it would be working!). But then I just did "Collider entity" in the OnTriggerStay/Enter/Exit Method and it still didn't print to the console that my player was entering. Why are my Triggers not working?
Click here for the code I'm trying
Click here to see how I have it in the Unity Scene
Triggers only respond to other colliders that have rigid bodies on them.
Try adding a Ridgidbody component to your player and set it to kinematic.
OnTriggerEnter/Stay/Exit works when the Object has a Collider Component and BluePuzzle2 doesn't have that.
Also OnTrigger function gets a Collider as parameter. Check the reference page
So in order to make that work put a script on every light and on that script copy this function
void OnTriggerEnter(Collider col) {
if (col.CompareTag("Player")) {
print("Entered the trigger");
}
}
Hope it helps.

Categories

Resources