Collision detection not working in Unity 2D - c#

I have two 2D game objects. They each have a Box Collider 2D and a Rigid Body 2D which is not kinematic. When the game plays, one moves towards the other and collides with it.
However, I also have the following method in the moving GameObject:
void OnCollisionEnter(Collision collision)
{
print( "Collided with someone" );
}
The print statement never prints, so presumably the method is never called. Where am I going wrong?

Unity has replicated all of the physics methods for 2D with the word "2D" stuck onto the end! So for your example, it should be changed to:
void OnCollisionEnter2D(Collision2D collision)
And the same with basically any other 2D physics thing.

Related

The weapon is not colliding with the AI

private void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log(collider.gameObject.tag);
}
When it collides with the weapon (or anything) it should print the tag. The problem is that it doesn't print anything.
Here you can see the player (normal colours) hitting the AI (green) and I am 100% that the AI is within the bounds of the weapons hitbox.
The AI layer and the player layers do interact with each other.
This is the box collider on the weapon (ignore it being disabled)
This is the box collider on the AI.
I have tried using ontriggerenter2D, ontriggerstay2D, ontriggerexit2D, ontriggerenter, ontriggerstay, ontriggerexit, oncollisionexit, oncollisionstay. I honestly have no clue why it is like this.
I know that it is not entering the on collision enter because it should be printing the ground tag that I have on the ground. Also the game is in 2D.
I believe the rigidbody or the collider have to be on the same game object as the script for the collision/trigger enter and exit methods to be called.
Please check the following:
The OnTriggerEnter2D(Collider2D collider) is on a script that
inherits from MonoBehaviour
This same MonoBehaviour script is in one of the GameObjects (Player
or AI)
This script is attached to a GameObject that also has a Collider2D
component
Last but not least...
Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached to it's GameObject as you can read it on the MonoBehaviour.OnTriggerEnter2D API

OnTriggerEnter unity3d

Please tell me, there is a function OnTriggerEnter:
void OnTriggerEnter(Collider other) {}
This function matches if an element is in the trigger of another element.
Now this script is on the character.
How can I perform this function for my character by hanging a script for example on terrain?
There is a lot of left out context in this question, and I hate to make assumptions, but I can't yet comment so I will try my best to answer. Assuming that you want to detect if the character is colliding with the terrain, the easiest way to do this, would be to instead use void OnCollisionEnter (). If your player character has a Rigidbody and a collider component, and your terrain has a collider component, you can tag your terrain "Terrain" and detect for collisions with it using the following function in your code:
//Detects a collision and grabs the collider
void OnCollisionEnter (Collider other)
{
//Detects if the object collided with has the "Terrain" tag
if (other.gameObject.tag == "Terrain")
{
//Your code here
}
}
Looks like you want to know how to use OnTriggerEnter
Here are the steps.
Attach Colliders to the objects that are expected to collider. At
least one of the Collider should be marked as trigger.
Attach a non-Kinematic Rigidbody to one of the objects.
attach the script with OnTriggerEnter function to one of the objects. The
function will be called if the collision is detected.
You can read this tutorial on Unity Collision for detailed explanation

Game object in triggered Box Collider 2D in unity

I have problem with 2d game in unity. There is sprite named Player with box collider 2D, rigidbody, moving... There is another sprite with two box colliders 2D. First isn't trigger, second is trigger. This sprite has this code:
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
Debug.Log("Player in range");
}
}
private void OnTriggerExit2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
Debug.Log("Player left range");
}
}
This is player
This is game object
I need to make that player in range of that object. This is only for debug but it's not working. I don't have any idea how to fix it. I tried docs but I don't know, I'm beginner. Thanks for help.
I see you are new to Unity. You have confused names of things. Let it put it straight for you.
Your Game Object is named Player
The Game Object's tag is Untagged
The Game Object has a Component called Sprite Renderer
The Sprite Renderer has a Sprite (the image) called playerDown_0
Your problem is that your game object Player does not have a tag.

OnCollisionEnter2D bug or missing some code

i am totally new to c# scripting and unity... i was trying something on unity and created this code and it's woring fine but some times it's just overlapping "wall" tagged object or even getting out of that circle i am using Edge collider 2D on it and polygon collider 2D on my shooter object and this script is attached to shooter object. check the screen shot for the bug.
void OnCollisionEnter2D (Collision2D collider){
if (collider.gameObject.tag == "wall") {
StartCoroutine (shooterscale());
collider.gameObject.GetComponent<bgAnimater> ().animateBg ();
if (turn) {
turn = false;
}
else {
turn = true;
}
}
}
I assume you are using Rigidbody2D on the shooter.
Make sure that Collision detection mode is set to Continous to avoid objects passing through each other.
From Docs:
When the Collision Detection is set to Continuous, GameObjects with
Rigidbody 2Ds and Collider 2Ds do not pass through each other during
an update. Instead, Unity calculates the first impact point of any of
the Collider 2Ds, and moves the GameObject there. Note that this takes
more CPU time than Discrete.
and
When you set the Collision Detection to Discrete, GameObjects with
Rigidbody 2Ds and Collider 2Ds can overlap or pass through each other
during a physics update, if they are moving fast enough. Collision
contacts are only generated at the new position.
Hope this helps

How do you make a 2d object go through another and detect there was a collision?

I'm creating a 2D snowboard game in which there will be snowballs that you will have to dodge, if you get hit by the snowball it will disappear and subtract the score.
I tried creating two game objects called Player and Ball and the code to detect the collision looks like this.
void Update
{
if (Player.transform.position.x == Ball.transform.position.x)
score--;
}
I'm not sure if I should create a OnCollisionEnter but i'm not sure how to do it.
Use OnTriggerEnter or OnCollisionEnter
http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

Categories