Help, im using unity 2020.3.15f2, OnCollisionEnter is not working.
I have 2 gameObjects, both with not trigger SphereColliders and not Kinematic RigidBodies.
My RigidBodies doesn´t have gravity on, but they have constants (don´t move on z, and don´t rotate on x or y)
Im shure they collide because both gameobjects interact colliding, but when i call the script that contains the OnCollisionEnter (that just call a Debug.Log("Collision")) i don´t see anything on console.
void OnCollisionEnter(Collision col){
Debug.Log("Collision"); //i don´t see anything on console
}
This is my SphereCollider and RigidBody setup for both GameObjects:
And the script is on the parent of those GameObjects, like this:
This is the Scene Hierarcy
And this is the "Element" Hierarchy (note: i edit it to mantain the names of my gameobjects in secret its my boss desition)
As you can see here the "Element" its the one who have the script with the OnCollisionEnter
I Solve by thinking: How my scene is setted?, I see that the gameObjects that contain the Collider and the RigidBody, are child of the gameObject with the OnCollisionEnter script.
Aparently, OnCollisionEnter only works with the gameObject itself, if the script is attached to a parent or to a child of the gameObject, it´ll not work.
So with that, i just need to make a Collision Detection Script on the gameObjects with the Colliders, and entangle it with the parent´s script to do... the thinks the gameObject should do when collide.
Thanks all for your feedback and help, special thanks for #derHugo and #Ruzhim who are the people who help me to detect that problem.
Solution: make shure the script with the OnCollisionEnter is on the gameObject that have the Collider and the RigidBody, not the parent, not the child.
Related
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
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 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.
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.
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.