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
Related
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.
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.
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 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.
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.