I have a object "A" with non convex mesh collider and kinematic rigidbodie and i wand to check trigger between Object "A" and another Game Object with non convex mesh collider and kinematic rigidbodie
As you see in this collision detection matrix:
Between two Kinematic the trigger is working and giving the message, so if you put both a trigger Collider it will give you a message.
If you want to be sure just add a tag to one and so you can also check the tag first and avoid wrong triggers:
void OnTriggerEnter(Collision other)
{
if(other.compareTag("Trigger2")
// do things
}
Related
I have my main character and added a capsule collider 2d so he would take damage when attacked. When the character is attacking, for example his body is out of the collider space and won’t take damage when hit. Please may you help me solve this issue, I’m very new to unity
One way of doing it is having two different hitboxes that does the same thing but with different shapes so they match two different states of your character.
Enable and disable them in a script when calling the attack using:
Public Collider2D attackCollider; //Drag your attack collider here in the inspector
Public Collider2D idleCollider; //Drag your idle collider here in the inspector
void AttackCollider() //call this fucntion to change to attack collider
{
attackCollider.enabled == true;
idleCollider.enabled == false;
}
void IdleCollider() //call this function to go back to normal collider.
{
attackCollider.enabled == false;
idleCollider.enabled == true;
}
If you want to I'm sure you can incorporate these two functions into an already existing attack function if you have one.
In 3D, you could simply make the Collider a child of any of your bones, it would get animated altogether.
But you mentioned Collider2D, so:
If you have a 2D Rig with bones, you can do the same as obove
If you have a Keyframe Animation, you could animate the collider to match your animation
I have a character GameObject which has 2 Colliders. Now I want to detect in my the control script of the character if one of these Colliders has a collision with a Collider of another object. Currently I'm trying to detect the collision like this:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("ObjectTag"))
{
Debug.Log("Collision with object");
}
}
For some reason the method is only called when I remove one of the Colliders of the character. The tag of the other GameObject is the same as the one in collision.CompareTag().
Does anybody know how to detect the collision of the character Colliders and the Collider of the other object?
https://answers.unity.com/questions/188775/having-more-than-one-collider-in-a-gameobject.html
Guess you have two of the same type of colliders, try changing to one box and one sphere or something along those lines
Hey so i am making a 2d game that is very similar to flappy bird (you jump when tapping the screen). The point of the game is to catch the green squares coming to you.
The problem i am encountering is that when my player hits the green square it seems to collide with it (it throws the player in some random direction or even stop into place like it hit a wall) even if on collision the green square is being destroyed.
So whenever the player hits the green square prefab it increments the score and destroys the square but sometimes when i jump right before hitting the square it stops like it collided with it insted of just passing right through.
The player prefab has got a rigidbody component and a 2d square collider.
The green square prefab has got only a 2d square collider component attached to it.
I am very new to unity and this is the first game i started developing.
If you need any further information please don't hesitate to ask.
Here is the player's rigidbody and 2d collider settings
Here is the green square's 2d collider settings
Maybe this picture of the gameplay can help. Imagine the blue square is the player and the green squares are the points you have to hit the red things are just the enemies that kill you so don't mind them. The player is standing still and the green square prefab have a script attached to it that adds a constant force to them. Hopefully this made it a lot clearer.
There is a really useful unity function that will detect when a collider touches another collider.
It is: void OnCollisionEnter(...)
You also have to pass in a Collider or Collider2d. So it will look like:
void OnCollisionEnter(Collider col)
or void OnCollisionEnter(Collider2D col)
Of course you have to have the necessary import statements for unity engine at the top as well.
You may want to use a trigger on your green Squares. then the bird is allowed to pass true but the square will activate your OnTriggerEnter code in your script.
so what you can try is:
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
//Run your code here!!!
//for example
Rigidbody rb = other.gameObject.GetComponent<Rigidbody>();
rb.AddForce(new Vector3(1, 1, 1));
}
}
this will add a force to your player/bird. but you can run any code in that method.
OnTriggerEnter and OnCollisionEnter run both when the object is hit by a rigidbody. but with OntriggerEnter you can pass thru the object. because it does not get effected by other physics objects. but onCollision usually does unless you freeze it in position.
side note OnTriggerEnter runs in FixedUpdate, so it is run at the end of each frame
I'm developing a simple 2D Unity game (which I'm very new to so sorry if this is a silly question!) in which my player can eat its enemies by colliding with them. This works fine as I'm just selecting the "is trigger" component for the enemies and using this code in my Player class:
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Hit detected");
Destroy(other.gameObject);
transform.localScale += new Vector3(x, y, z);
}
However, this means the colliders placed around the border of my background image aren't stopping the enemies. What's the best fix for this?
I don't understand very well your question. However it seems that your collisions are not working. So, remember that for have collisions actually taking place in your game you need to use colliders and that one of the two elements participating in the collision need to have the rigidbody component.
That will make the physics work in the engine, which triggers dont.
To check if that works you can debug with:
// called on collision
void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("OnCollisionEnter2D");
}
From what I understood, you want only to detect triggers between the player and the enemies, but you still want these to collide with physic objects in your scene, such as background colliders.
One possible way to achieve this is to create a child object for the player object with a collider component with the trigger option set, and attaching a script to it to handle the triggers. Then, with the use of layers to group your player and enemy objects, you can uncheck the collision between them following: Edit -> Project Settings -> Physics 2D: "Layer Collision Matrix".
You can assign a script to any enemy, checking the distance with the player in each frame. Then you can Uncheck "is trigger"
Vector2.Distance
I have a cube tagged "player", which needs to jump at certain locations, represented by Empty GameObjects.
I want to make a point system and that why I put on some of the places other game objects tagged "Coin"
The cube has an animation which plays each time it moves to another point recognized by clicking with the mouse on it.
When I jump on the coin location I want the Coin object to get destroyed.
I use this function:
void OnTriggerEnter(Collider col){
if (col.gameObject.tag == "Coin") {
Debug.Log ("Plm");
stroy (col.gameObject);
}
The problem is that when I'm jumping at the Coin location nothing happens.
I have box colliders on both objects and OnTrigger checked on both.
1) Make sure one gameobject has a rigidbody
To have the OnTriggerEnter happen you need at least one of the two object to have a RigidBody attached to it.
2) Static Trigger Collider
An object that has a collider and isTrigger checked but no rigibBody is considered a Static Trigger Collider which are From Unity Documentation:
This is a GameObject that has a Collider but no Rigidbody. Static colliders are used for level geometry which always stays at the same place and never moves around. Incoming rigidbody objects will collide with the static collider but will not move it.
This type of collider interactions is perfect for your coins because they do not move and stay static.
3) Rigidbody Trigger Collider
The next type of collider interactions is a Rigidbody Trigger Collider which are :
This is a GameObject with a Collider and a normal, non-kinematic Rigidbody attached. Rigidbody colliders are fully simulated by the physics engine and can react to collisions and forces applied from a script. They can collide with other objects (including static colliders)
This type of collider is what you need on your player and as it says in the Unity Documentation it will be able to collide with the Static trigger collider.
Important observation:
Having IsTrigger set to true on your player collider will cause your player to not respond as a solid object
A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through.
which might not be the most optimal solution for you because most of the time you want your player to be able to collide with other things like the ground. So you can set the isTrigger to false and just have a rigidbody on you player and it will react to the coins if they're set as Static Trigger Collider.
If you want an easy way to know what can collide with what Read the colliders documentation and take a look at this chart that shows it in a more simpler way.