Unity 2D On Collision (Do Something) not working? - unity3d

I know this question has probably been asked many times, but I'm going to ask again.For some reason, my colliders won't work. I have one on my block sprite, and another on my 'miner' sprite. When I hit play, the two start on top of each other. (I'm not sure if this matters, it appears to make no difference.) Each one has a Box Collider 2D. In the script assigned to one of them, it tries to see the collision.
void OnCollisionEnter2D(Collision2D collision)
{
if (col.gameObject.name == "Miner")
{
GameObject.Find("Miner").GetComponent<miner>().block = block;
}
}
However, this script does not seem to detect the Miner sprite colliding with it. I am certain I am deriving from MonoBehavior, so it's not issue there.

Do either of the GameObjects have a RigidBody2D attached to them? At least one GameObject needs to have a RigidBody in any given collision.

Add some debug statements in your code so that you are sure this method is not running. You should read up here about Collisions in unity. Decide if these objects will be moving and set the appropriate collider. As stated above you will typically need a rigidbody.

Related

Using triggers attached to enemy to make enemy jump in a 2.5d platformer

so I've got an enemy in my 2.5d platformer, that I already got to follow the player around, now I want him to jump over small obstacles, and honestly, I have no idea how to. My enemy is a sphere with rigidbody and a box collider scaled 2x in X axis, effectively making it 2 times wider than the sphere. I want this box collider to be a trigger and for it to launch a AddForce.
Where do I put the code for the trigger - if I put it in void update() wouldn't it apply force until the sphere is above the obstacle? I only want it to apply the force once.
Also, what would the code for the trigger look like?
So far I only got figured out how to find the rigidbody by getcomponent and apply force to it, and I watched a video on triggers, but it didn't really help :/
Use OnTriggerEnter method like this:
void OnTriggerEnter(Collider other)
{
if (other.tag == "IMakeEnemyJumpBeforeAnObstacle")
{
// add force here
}
}
Note: this code should be in your enemy script. Plus use IMakeEnemyJumpBeforeAnObstacle tag on the trigger object which will be placed right before the obstacle. And make sure both the enemy and the other object have property IsTrigger checked from inspector.
Learn More
Hope this helps

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!

Determine on which collider the collision has taken place

I have a gameobject with two sphere colliders attached. One has IsTrigger checked and the other not.
I want to execute different set of statements when collision occurs with different colliders. For example I want to play different sound for both different collisions. Is there any way to achieve it?
I tried OnTriggerEnter() but unfortunately it is called for both type of collisions since other colliding objects have triggered colliders. I just thought if we could somehow find out on which collider of the gameobject the collision has taken place we will be able to achieve it.
So is there any way to get through with this?
I have been using Unity for years and faced tons of problems like this, related to bad software design. I hope Unity guys will handle physics more carefully in future releases.
In the mean time, you can use Physics.OverlapSphere and Physics.CheckSphere to manually check if there is something that collides with your object. Remove the collider that you are using as a trigger and use these methods instead of OnTriggerEnter. This is a bit hacky, but this will do the job I think.
Make your colliders visible in the inspector (make them public or add [SerializeField] before it) and then tie in the colliders to the code that way.
Then, in your collisions, compare the colliding objects against your variables that are holding the colliders for you to keep them separate.
To detect for source trigger in OnTriggerEnter, you must use workaround with multiple gameobject hosting trigger and satellite scripts.
Allow me to link to my answer on gamedev SO:
https://gamedev.stackexchange.com/a/185095/54452

In Unity3D How to Detect the Collider Touch and Stay In Other Collider?

I need to check if the sphere collider still stay in the mesh collider.
now I used:
void OnCollisionEnter(Collision info)
{
Debug.Log("enter");
}
void OnCollisionStay(Collision info)
{
Debug.Log("stay");
}
void OnCollisionExit(Collision info)
{
Debug.Log("out");
}
here is the scene:
the onCollisionStay() will execute per frame ,but when the scene came to before,the Debug do not log continue,but the "Out".
So I need to Know Why the Stay event do not Stay and How to detect One Collider
Stay in the another just inside and touch?
Increase the size of Sphere collider or use 'Delay' option in Collision Stay function. Your question is bit clumsy, What your are trying to do, tell it clearly.
Can you give more details of the mesh collider? It might happen that the collider is pretty small also, and in some point, a part of the sphere (not the full sphere, so you think OnCollisionStay should still be called) goes out of the mesh collider, thus activating the OnCollisionExit(), but since the OnCollisionEnter() is not called back, OnCollisionStay() is never called again.
Maybe you can check that.

Collision of object with player

So I have a bit of an issue with some unity scrip. I have a moving object, a ceiling to be specific, and I have it lower and 'crush' the player. However, it's not working. The ceiling moves, but nothing happens when it collides with the player. It used to work if the player was moving when it hit them but it isn't any more and I haven't changed any of the script so I don't know why that's happening.
Interesting suggestion from your teacher. I would put a character controller on your player, which you seem to have already. Then I would add the collision code:
void OnCollisionEnter(Collision c)
{
// die.
}
Then add a box Collider and Rigibody to the roof. I would also add this code above to the roof.