Game object in triggered Box Collider 2D in unity - c#

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.

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

Detect Wheel Collision Unity 3D

I have a car in my scene that has Wheelcollider attached to it's wheels, I want to detect when the wheels collide with another object (with box collider and rigidbody component), I tried attaching the script bellow to the wheels but it didn't detect the collision, I've also tried attaching it to the object, but it didn't succeed, how can I solve this?
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Wheel"))
{
Debug.Log("Collided");
}
}

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

collision not triggered when i touch object

I have 1 First Person Controller as a player and created 1 cube as an enemy.
Enemy has rigidbody to assign gravity to it and it moving on 1 platform which is made from cube.
collision detect script is attached with enemy. when enemy touches the player method triggered successfully. but when Player touch enemy method is not called.
void OnCollisionEnter(Collision collision) {
Collider other=collision.collider;
Debug.Log(string.Format("OnCollisionEnter tag={0}",other.tag));
//
if(other.gameObject.tag=="Player")
{
Debug.Log("Player1 Touched");
}
}
I found the answer.
as I have used firstpersoncontroller i have to implement below method to find collision
void OnControllerColliderHit(ControllerColliderHit collision)
{
Debug.Log(string.Format("object tag={0}",collision.collider.gameObject.tag));
}
Thank you all for your help.
unity link that helped me.
http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnControllerColliderHit.html

Collision detection not working in Unity 2D

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.

Categories