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.
Related
I have a game in which waves of enemies are spawned by storing an arrangement of enemies as children of an empty game object, which I can then save as a prefab and instantiate with an enemy spawner script, but there is no collision between the enemies, and the only reason I can think of is because of some physics optimization due to them being siblings. These are my collider and rigidbody settings for the enemies: Any idea why this happens? Thanks!
This is likely because your Rigidbody constraints are set to freeze movement on both the X and Y axes:
This means that any movement you would normally expect from simulated physical interactions (for example, changing velocity from colliding with another enemy) will be ignored for this Rigidbody. See the Unity docs for more details on what they do.
The simple fix here would be to uncheck those checkboxes, so that you rely on the physics system to handle these interactions. If you want to fully control them through code though, you could technically write code in OnCollisionEnter() to handle the collision between two enemies - but that would be a much more involved solution.
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.
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.
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!
I have a cube tagged "player", which needs to jump at certain locations, represented by Empty GameObjects.
I want to make a point system and that why I put on some of the places other game objects tagged "Coin"
The cube has an animation which plays each time it moves to another point recognized by clicking with the mouse on it.
When I jump on the coin location I want the Coin object to get destroyed.
I use this function:
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Coin") {
Debug.Log ("Plm");
stroy (col.gameObject);
}
The problem is that when I'm jumping at the Coin location nothing happens.
I have box colliders on both objects and OnTrigger checked on both.
1) Make sure one gameobject has a rigidbody
To have the OnTriggerEnter happen you need at least one of the two object to have a RigidBody attached to it.
2) Static Trigger Collider
An object that has a collider and isTrigger checked but no rigibBody is considered a Static Trigger Collider which are From Unity Documentation:
This is a GameObject that has a Collider but no Rigidbody. Static colliders are used for level geometry which always stays at the same place and never moves around. Incoming rigidbody objects will collide with the static collider but will not move it.
This type of collider interactions is perfect for your coins because they do not move and stay static.
3) Rigidbody Trigger Collider
The next type of collider interactions is a Rigidbody Trigger Collider which are :
This is a GameObject with a Collider and a normal, non-kinematic Rigidbody attached. Rigidbody colliders are fully simulated by the physics engine and can react to collisions and forces applied from a script. They can collide with other objects (including static colliders)
This type of collider is what you need on your player and as it says in the Unity Documentation it will be able to collide with the Static trigger collider.
Important observation:
Having IsTrigger set to true on your player collider will cause your player to not respond as a solid object
A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through.
which might not be the most optimal solution for you because most of the time you want your player to be able to collide with other things like the ground. So you can set the isTrigger to false and just have a rigidbody on you player and it will react to the coins if they're set as Static Trigger Collider.
If you want an easy way to know what can collide with what Read the colliders documentation and take a look at this chart that shows it in a more simpler way.