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
Related
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.
I am using Unity 2019.4.17f1 and I have two objects with colliders and I want to detect if one of them collides with the other, so OnCollisionEnter should be called on both objects (although I really only care if it is called on the other cube because the first one will just be a controller so that the second collider does something once it detects that the controller collides with it). To do the test I have this scene with three cubes:
And a component that is included in the three cubes with the next code:
public string text;
private void OnCollisionEnter(Collision collision)
{
Debug.Log("Collision made: " + text);
}
Each of them has a text variable with the name of the colour they have so when they collides they will show the text with the colour that is colliding, there are also only two rigidbody there, one inside Blue Cube and other inside Rb GameObject, both of them has no gravity.
But as you can see in the previous picture only Blue and Red debug text are shown, so when Blue is colliding with Green only Blue reacts and if Blue collides with Red both Blue and Red are shown. Why it reacts different if the object is child of a rigidbody and what can I do to make that Green also reacts to the collision?
EDIT:
I also test if the Red cube have a Rigidbody on it and it works as expected (Blue and Red messages appears) so I don't understand why Green doesn't work too.
EDIT2:
This is the Rb GameObject:
And the Green GameObject:
From the Unity colliders documentation
Compound colliders approximate the shape of a GameObject while keeping a low processor overhead. To get further flexibility, you can add additional colliders on child GameObjects. For instance, you can rotate boxes relative to the local axes of the parent GameObject. When you create a compound collider like this, you should only use one Rigidbody component, placed on the root GameObject in the hierarchy.
Make sure that your DetectCollider script and RigidBody component are attached to the root GameObject which in this case is Rb. Collision events get passed from colliders to attached or parent RigidBody so your CollisionScripts should be attached accordingly. Also make sure that Green GameObject doesn't have Rigidbody attached so the events get passed to parent Rigidbody in Rb instead.
In this case you have DetectCollision attached to child Green gameobject but RigidBody attached to Rb which means Collision Events get passed to Rb GameObject and not to Green gameobject.
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.
My player currently dies when it hits an enemy tagged with "Enemy" and restarts the level using the following script
if (other.gameObject.CompareTag("Enemy"))
{
Destroy(gameObject);
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
I wanted my player to kill the enemy when the bottom of my player hits the enemy, so i created a child gameobject called "Feet" and labelled it "Feet" as well, I also added an edge collider to it. I added the following script to it.
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
Now the "Enemy" dies when the "Feet" collides with it, but my player also dies, how do i make an exception to the first script so my player doesnt die when "Feet" collides with the object first instead of the "Player"
You should restrict the collider of the player. It seems like your "Feet" is included in the "Body" of the player. So "Feet" and "Body" touch the Enemy at the same time.
So make the collider of the player smaller at the bottom.
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.