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.
Related
I have this empty object with a collider attached, and it follows the mouse around. The "is trigger" box isn't checked, and that's it.
Now, on my other Object, I also added a collider (2d of course) and set it as "is trigger" = true, but I also tried it with the box unchecked.
The following Code should execute when the Trigger "area" is entered:
public void OnTriggerEnter2D(Collider2D collider)
{
print("found coin");
manager.AddCoin();
Destroy(gameObject);
}
But it does not. How do I fix this?
It sounds that you need a RigidBody2D attached to your first gameObject (the one you're moving around). To detect collisions, at least one object must have this component (get more info in the Docs)
I think that you need one of your objects to have a Rigidbody2D attached to it in order for the physics engine to really engage and detect collisions (even triggers).
The documentation seems to imply that:
https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html
"Note: Trigger events are only sent if one of the Colliders also has a
Rigidbody2D attached. Trigger events are sent to disabled
MonoBehaviours, to allow enabling Behaviours in response to
collisions."
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
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){}
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.
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.