Why won't Unity 2017.3.0f3 recognise Ontrigger2D /OnCollision2D? - c#

I have a 2d box collider on a "Magnet" sprite and a circle collider on a "Ball" sprite. On the Magnet sprite, I turned on "Is Trigger" and tried a few rigidbody settings: Kinematic, Static and Dynamic. The Ball also has a Dynamic rigidbody on it. I'm using Unity 2017.3.0f3.
For the time being I just want to check if an object has entered the Magnets collider and log the object's name.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Magnetism : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
Debug.Log(other.name);
}
}
I also tried using the OnCollisionEnter(Collision other) and it still doesn't do anything. Oddly I have no errors as well. Oh, also in one forum, I found someone mention that using a onCollisionEnter/onTriggerEnter. That didn't work for me either.

Here is the checklist to make sure your collision works
Make sure the layers your gameObjects are in are set to collider under Project Settings -> Layer Collision Matrix.
Make sure at least one of the colliding bodies have a Rigidbody2D (not rigidbody, must be 2D) attached to it
Both of the objects must have a Collider2D
Make sure the Magnet's collider has IsTrigger set to true
The script in which the IsTriggerEnter2D function is present must be on the same object which has the collider2D (Also make sure this collider is set as a trigger)
Make sure the size of the colliders isn't 0 or is big enough. This can happen if you create a 2D sprite first and put the actual sprite image later
Make sure all of the components and gameObjects are enabled/active
I know some of them are trivial but still easy to miss
Edit: One more thing; the rigidbody2D must have simulated set to true.

I've got two objects (magnet with box2D and ball with circle). The magnet object has a script attached that does the same as you. Everything works as it should. (Tested with your version.)
Just try to double check everything and otherwise, try to create a new project and see if it occurs. If that doesn't work, test with 2017.3.1f1. If all that does not work, with I highly doubt, send a bug report to Unity and they'll help you.

Ensure that all colliders and rigidbodies you're using are 2D.
In order for the OnTriggerEnter2D to work, at least one of the colliders must have the Is Trigger active.
To make a quick test, you can disable all rigidbodies and check with only the colliders active.
The script code is perfectly fine and should work.

Related

unity c# OnCollisionEnter2D() Not working

I can't get a collision detection to work between and Enemy object and a Projectile object. I tried looking online for anyone else having similar problems, but haven't been able to find anything that relates to the problems I'm having.
I'm not getting any errors in the console or warnings.
In my c# code that's a component to and object with a Rigidbody2D and CircleCollider2D I have
void OnCollisionEnter2D(Collision2D collision)
{
print("collision");
if (collision.transform.tag == "Projectile") {
print("Collision with Projectile");
}
}
Not getting collision prints from the code above to print to console.
I have another object that has RigidBody2D and CircleCollider2D as well. It also has the tag "Projectile". When the collider areas overlap with each other nothing happens.
Enemy: RigidBody2D, CircleCollider2D, C# code above.
Enemy:Tag: "Projectile", RigidBody2D, CircleCollider2D: IsTrigger - true.
Have you checked your rigidbody properties?
I know some of the properties that you can change on that effect the way collisions work, try turning off simulated if that one is on for a start.
You have isTrigger checked in collider component. OnCollisionEnter doesn't work when trigger is enabled. There's another function OnTriggerEnter if you want to use trigger.
OnTriggerEnter
OnCollisionEnter
Don't confuse these two.

Unity C# capsule collider and rigid body don't trigger using transform.position to move

I have 2 simple capsules. 1 is stationary and the other is moving to the same tile on the tilemap using transform.position.
Both capsules have capsule colliders and rigid bodies. I've attempted to remove the rigid body but from what I can tell, the OnCollisionEnter function requires a rigid body to work.
My script, attached to both of these, is a simple:
private void OnCollisionEnter(Collision other)
{
print("Collision Detected! " + other.gameObject.name);
}
I've used combinations of 'isKinematic', 'isTrigger', placing one component before the other then reversing, ensuring I have the script attached to both objects, making sure my capsule collider is sized to the object, and looked at other people's issues to try their steps to fix my issue.
Nothing seems to trigger the collision between the 2 capsules. It's got to be something small I'm missing.
I have a tag on both objects and have tried things like if(other.gameObject.tag == "myTag") {...} but that's not working for me, either.
Can anyone spot my mistake? Let me know if you need to see anything else - happy to provide images or whatever will help. Thanks in advance!
Above is the basic identical hierarchy of both capsules with components attached to the capsule mesh. Both have the parent empty objects they're stationed under. (Which themselves, contain scripts but no collider or rigid body)
Here is an infographic to show when a collision message will be detected by OnCollisionEnter between two objects. Both objects will need some sort of collider, and will most likely need a Rigidbody.
You will not want to set isTrigger as that will not make it physically react to a collision, but will just detect when a collision occurs. It will also not call OnCollisionEnter but will call OnTriggerEnter. Setting both not as triggers, adding a collider, and giving them Rigidbodies should allow the collision to be detected. You will also need to attach this script to one of the objects that have the collider. Are there other components on the objects you are using?
Make sure you are not looking for
void OnTriggerEnter(Collision collision){}

cant make my gameobjects change position after collision [duplicate]

First, I know that this question has been asked a lot, but I cant find a solution, so mi problem is, Im making an educational game, and I have a vein and the blood flow (with many box colliders) and a single blood cell (also with a box collider) however i want the cell to destroy when it reaches the wall collider, but it doesn't it just stays there, here is the project!
http://tinypic.com/r/10706es/9
(cant upload images because of my reputation, sorry)
The collider where I want to destroy my cell is the pink collider, however when it touches it it just does nothing, here's my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collision : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnCollisionEnter(Collision col)
{
print("hihi");
if (col.gameObject.tag == "Collider")
{
Destroy(gameObject);
}
}
}
Also, here is the AddForce script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddForce : MonoBehaviour {
public float thrust;
public Rigidbody rb;
private Vector3 up;
private bool move;
void Start()
{
rb = GetComponent<Rigidbody>();
up = new Vector3(0, 1, 0);
move = false;
}
void FixedUpdate()
{
if (Input.GetKey("space"))
{
if (rb.velocity.magnitude < 5)
rb.AddForce(up * thrust);
move = true;
}
else
{
if (move == true)
rb.velocity = new Vector3(0, -0.5F, 0);
}
}
}
thanks for your help guys! :D
It can be several things, whether you are using OnTriggerEnter or OnCollisionEnter:
Missing RigidBody (the most common). At least one of the GameObjects involved needs to have a RigidBody. (check if at least one of them have a RigidBody attached and, if you are using OnCollisionEnter, does not have the "Is Kinematic" checked). See the below collision matrix for more information.
Missing tag. The GameObject from collision does not have a "Collider" tag (try to remove the if statement to test it) (to compare tags, use collider.gameObject.CompareTag("Collider"), it has a better performance)
Undetectable collision. The physics Layer Collision Matrix is set to not detect collision between the layers the objects are (enter Edit > Project > Phisics and check if the encounter of the layer of both GameObjects are checked inside Layer Collision Matrix)
Wrong Collider configuration. one or both of the GameObjects have a small/wrong placed or absent Collider (check if they both have a Collider component and if their size are correct)
If it's working, you should be able to press play and drag one GameObject into the other one and your Debug.Log will appear.
As an advice, use tag names that better describe the group of GameObjects that will be part of it, like "RedCells" or "WhiteCells". It'll be easier to configure the Layer Collision Matrix and improve the performance of your game.
Another advice: for colliders that just destroys another GameObject (don't react, like bump or actually collide) I use triggers. That way, the collision between them will not alter anything in the remaining GameObject (like direction/velocity/etc). To do that, check the Is Trigger in the Collider and use OnTriggerEnter instead of OnCollisionEnter.
Source
some times you added Nav Mesh Agent component to your game object ( to auto route operation in strategic game and ...).
in this case, this game object does not attend to collider.
So, if you really need this Nav Mesh Agent, you should add Nav Mesh Obstacle to other fixed game object and also add Nav Mesh Agent to other movable game object.
I have a few followup questions which might lead to a solution.
First, does the object holding your 'collision' script have a rigidbody and a collider on it?
Second, does the wall have both a rigidbody and collider?
Usually if those conditions are met, then collisions will work.
A couple other things that could be the problem:
Check if you have istrigger checked for either object and make sure it is unchecked.
Check and make sure the rigidbodies on both are non-kinematic.
I've finally fixed it, I dont really know if this was the problem, but i just removed the rigidbody from the parent of the wall and it started working!, I dont know what the rigidbody did, but just with that the problem was fixed, thank you all for your help! :D
Ensure the following things are considered in your code,
All gameobject should contain collider attached and the player
gameobject should contain the rigidbody component in it.
The collider size should change to the width and height of the
component instead of default (1,1) values.

Why is my bullet moving through my enemy and not detecting a collision?

I have my Unity 2D project set up so that the bullet is a trigger and will detect and collisions but not interact with the environment, and my enemy being a collider so that it will do both. I don't know if the fact that they are two different types affects collisions, but I thought I'd include that detail incase it did. I know this issue is not caused by my bullet moving too quickly since when I turn the speed of the bullet down the issue persists. I will include the code for the enemy collision and the bullet collision detection here:
void OnCollisionEnter2D (Collision2D coll)
{
if (coll.gameObject.tag == "Proj") {
Destroy(gameObject);
Debug.Log("Hit");
}
Now for the bullet:
void OnTriggerEnter2D(Collider2D hitInfo)
{
if (hitInfo.gameObject.tag == "Enemy") {
Destroy(gameObject);
}
The bullet is detecting a collision with an enemy and deleting itself, but the enemy is not detecting a collision with the bullet and taking damage/deleting itself. Even when I turn off the Destroy(gameObject) part of the bullet code so that it doesn't destroy itself on collision I still have the same problem so I know it isn't matter of it deleting before anything detects. I hope I was thorough enough with my explanation and that someone can help me resolve this issue.
Image of the inspectors: https://imgur.com/a/TLfcNcp
The bullet needs to be a non-trigger collider as you are calling OnCollisionEnter and not OnTriggerEnter from the enemy script.
Please show the settings of each collider, also OnCollisionEnter2D and OnTriggerEnter2D are 2 different things, and will occur in different conditions, so make sure that you are using the right one for the player.
I'm not entirely familiar with trigger behaviour, as it's been a while since I've played around with those, but this bit of documentation for MonoBehaviour.OnTriggerEnter2D(Collider2D) is an important starting point for solving your problem:
Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached.
I can't say for sure, but your screenshots suggest that there is no Rigidbody2D involved.
Interestingly,
documentation for Collider2D.OnTriggerEnter2D(Collider2D) neglects to specify that a Rigidbody2D is necessary. I'm not sure what this implies; both MonoBehaviour.OnTriggerEnter(Collider) and Collider.OnTriggerEnter(Collider) (the 3D analogues) are said to require a RigidBody. Both of these pages also claim
If both GameObjects have Collider.isTrigger enabled, no collision happens.
However, the example code from Monobehaviour.OnTriggerEnter2D(Collider2D) claims that setting both 2D colliders as triggers will still allow the function to be called.

Collision detection not working unity

First, I know that this question has been asked a lot, but I cant find a solution, so mi problem is, Im making an educational game, and I have a vein and the blood flow (with many box colliders) and a single blood cell (also with a box collider) however i want the cell to destroy when it reaches the wall collider, but it doesn't it just stays there, here is the project!
http://tinypic.com/r/10706es/9
(cant upload images because of my reputation, sorry)
The collider where I want to destroy my cell is the pink collider, however when it touches it it just does nothing, here's my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collision : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnCollisionEnter(Collision col)
{
print("hihi");
if (col.gameObject.tag == "Collider")
{
Destroy(gameObject);
}
}
}
Also, here is the AddForce script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AddForce : MonoBehaviour {
public float thrust;
public Rigidbody rb;
private Vector3 up;
private bool move;
void Start()
{
rb = GetComponent<Rigidbody>();
up = new Vector3(0, 1, 0);
move = false;
}
void FixedUpdate()
{
if (Input.GetKey("space"))
{
if (rb.velocity.magnitude < 5)
rb.AddForce(up * thrust);
move = true;
}
else
{
if (move == true)
rb.velocity = new Vector3(0, -0.5F, 0);
}
}
}
thanks for your help guys! :D
It can be several things, whether you are using OnTriggerEnter or OnCollisionEnter:
Missing RigidBody (the most common). At least one of the GameObjects involved needs to have a RigidBody. (check if at least one of them have a RigidBody attached and, if you are using OnCollisionEnter, does not have the "Is Kinematic" checked). See the below collision matrix for more information.
Missing tag. The GameObject from collision does not have a "Collider" tag (try to remove the if statement to test it) (to compare tags, use collider.gameObject.CompareTag("Collider"), it has a better performance)
Undetectable collision. The physics Layer Collision Matrix is set to not detect collision between the layers the objects are (enter Edit > Project > Phisics and check if the encounter of the layer of both GameObjects are checked inside Layer Collision Matrix)
Wrong Collider configuration. one or both of the GameObjects have a small/wrong placed or absent Collider (check if they both have a Collider component and if their size are correct)
If it's working, you should be able to press play and drag one GameObject into the other one and your Debug.Log will appear.
As an advice, use tag names that better describe the group of GameObjects that will be part of it, like "RedCells" or "WhiteCells". It'll be easier to configure the Layer Collision Matrix and improve the performance of your game.
Another advice: for colliders that just destroys another GameObject (don't react, like bump or actually collide) I use triggers. That way, the collision between them will not alter anything in the remaining GameObject (like direction/velocity/etc). To do that, check the Is Trigger in the Collider and use OnTriggerEnter instead of OnCollisionEnter.
Source
some times you added Nav Mesh Agent component to your game object ( to auto route operation in strategic game and ...).
in this case, this game object does not attend to collider.
So, if you really need this Nav Mesh Agent, you should add Nav Mesh Obstacle to other fixed game object and also add Nav Mesh Agent to other movable game object.
I have a few followup questions which might lead to a solution.
First, does the object holding your 'collision' script have a rigidbody and a collider on it?
Second, does the wall have both a rigidbody and collider?
Usually if those conditions are met, then collisions will work.
A couple other things that could be the problem:
Check if you have istrigger checked for either object and make sure it is unchecked.
Check and make sure the rigidbodies on both are non-kinematic.
I've finally fixed it, I dont really know if this was the problem, but i just removed the rigidbody from the parent of the wall and it started working!, I dont know what the rigidbody did, but just with that the problem was fixed, thank you all for your help! :D
Ensure the following things are considered in your code,
All gameobject should contain collider attached and the player
gameobject should contain the rigidbody component in it.
The collider size should change to the width and height of the
component instead of default (1,1) values.

Categories