Getting the trigger Object [PONG Game] - unity3d

i was making a pong game but i am coming across a problem. When my ball (sphere collider) collides with one of the side walls (Box collider - trigger object), i want to know which wall it collided with (i put that tag in both walls). The problems is, OnTriggerEnter() method only tracks the object that is not the trigger. How should i get the information on which wall my ball collided with? I need to specificily know whether it was wall a that the ball collided with or was it walla b that the ball collided with. Any help would be greatly appreciated.

You have the gameobject inside the collider
void OnTriggerEnter(Collider other) {
Debug.Log(other.gameobject.name);
}

Related

Unity : How to handle compound colliders 2D physics

I´m making a small game where the basic game mechanic is dragging things around the level . My Player G.O. is the one who drags the other objects around the level . This G.O. (the player GO) has a rigidBody2D component , and in order to drag other objects , I turn the dragged object into a child of the player , so that I only move the player through the rigidBody component and the dragged object moves along the player object .
The problem is that whenever something that should kill the player by contact makes contact with the object that is being dragged , the death of the player still triggers even though the player has not make contact with the hazardous object to begin with . This is obviously not the intended result , if something that is meant to kill the player makes contact with the dragged object but not the player , the player should not be killed . How can I fix this?
EDIT : By killing I mean destroying the player GO.
If you delete your parent GameObject, all its child GameObjects will delete with it too. In order to not get your dragged objects deleted, you should remove them somewhere else before deleting the parent GameObject. You could do that by:
YourChildGameObject.transform.SetParent(OtherParentGameObject, false);
Also, this link could help you.
This is a problem with collisions.
Set different layers to player and enemies.
Go to Edit -> Project Settings -> Physics. On the bottom, you'll see a list of collisions. You can check which layers collide with which layers there. Set the "enemy" layer to collide only with the "player" layer.
Another way to do this is by checking what is the enemy colliding with OnCollisionEnter or OnTriggerEnter (depending on how you're doing it). You can do it like this:
//You can change this to OnTriggerEnter and "Collision" with "Collider"
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "player")
{
//Kill the player
}
}
Good luck!

Unity 2d 5 Collider not working

Using Unity 5.0.1f1
I'm trying to make it so that when I shoot, if it hits an invisible object it destroys it, but when it collides nothing happens. Here is the code:
void OnTriggerEnter2D(Collider2D col){
Destroy (col.gameObject);
Debug.Log ("find");
}
It's hard for me to give you an answer given the little information that you are providing. But, the most possible solutions to your problem are:
Attaching a Rigidbody component to one of the colliding objects.
Making the object which will be destroyed have a normal collider attached and the other object must have a trigger collider attached.
Make sure that all of the Colliders/Triggers and Rigidbodies you use are 2D. That is, Rigidbody2D and Collider2D. Because you are using the void OnTriggerEnter2D method.
Make sure that the colliding objects are in layers which collide with each other. You can check which layer collides with each layer by going to Edit-->Project Settings-->Physics2D (Or Physics if you end up using 3D Physics).
Hope this helps!

Unity 2D: How would I go about making a block that shoots when the player touches it?

I am making a 2D platformer in Unity and I want to make a block that works very similarly to the shooting blocks from Kid Chameleon. When they are touched by the player, they shoot from what every direction the mark is on and the bullet can hurt the player and enemies, destroy crates and activate other shooting blocks. Can some one tell me how I can achieve this because I have no idea where to start?
For start you can use OnMouseDown() event for detecting a click(or touch) on an object and then you can Instantiate a bullet object from block and give it's rigidbody a velocity to move towards that. and you can use OnCollisionEnter2D(Collision2D c) event for every Gameobject that you want to detect collision and do your job.

Unity OnTriggerEnter2D sometimes doesn't work

I have problem with triggers in my 2D game in Unity. I want to make enemy die when he triggers with player's weapon. The problem is there are two colliders attached to enemy (tagged "Enemy"):
one is box2d collider which is used as normal collider
second is sphere collider which is set as trigger and is used in script to check whether there is a player in range
I got sword object, which has sprite renderer, box collider (set as trigger) and script:
void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Enemy")
{
if(!other.isTrigger)
{
Debug.Log ("enemy");
Destroy (other.gameObject);
}
}
}
Screenshot of scene:
http://i.stack.imgur.com/eVtRX.jpg
Screenshot of Enemy gameObject:
http://i.stack.imgur.com/9R5a6.jpg
So in general it sometimes works, but sometimes doesn't. When I disable sphere collider at enemy, everything works great, but I need to have it to check if there is player in range. How can I fix it?
you should Make sure two things in OnEnterCollider2D
1) Make sure both the gameobjects participating in OnEnterCollider2D must not be destroyed. If one has to be destroyed then it should be destroyed with some time later.
2) Make sure one of the game object participating in collisions has to have rigidbody attached with isKinematic unchecked.

Collisions issues of CharaterController with other box colliders

I have a Player with a CharacterCollider & Coins with box collider. When my player collides with coin, then i m disabling coin in OnControllerCollideHit() with hit.gameObject.active = false (where hit = coin gameObject).
But still i m getting some back force or a kind of jerk when i collide with coin.
How can i remove that jerk or force on player collision with any coin box collider.?
I did a lot of research on Google & some forums, but can't find related to this issue.
Any Code will be appreciated.
Thankx
I resolved this issue with a trick.
I added an empty child GameObject with Box collider & Rigidbody in my Player GameObject & increase the collider area that cover my player collider.
This will make me to react before i collide with player collider. And i m handling Coin collision & other collision with empty GameObject collider.
I think this solution might probably help other people on Here. Gud Luck.
If you Don't use Trigger, I suggest check on trigger on Box collider in your Player Object. then OnTriggerEnter function will called when collide with coin.