Using Unity 5.0.1f1
I'm trying to make it so that when I shoot, if it hits an invisible object it destroys it, but when it collides nothing happens. Here is the code:
void OnTriggerEnter2D(Collider2D col){
Destroy (col.gameObject);
Debug.Log ("find");
}
It's hard for me to give you an answer given the little information that you are providing. But, the most possible solutions to your problem are:
Attaching a Rigidbody component to one of the colliding objects.
Making the object which will be destroyed have a normal collider attached and the other object must have a trigger collider attached.
Make sure that all of the Colliders/Triggers and Rigidbodies you use are 2D. That is, Rigidbody2D and Collider2D. Because you are using the void OnTriggerEnter2D method.
Make sure that the colliding objects are in layers which collide with each other. You can check which layer collides with each layer by going to Edit-->Project Settings-->Physics2D (Or Physics if you end up using 3D Physics).
Hope this helps!
Related
I am running into an issue where OnTriggerEnter does not seem to be getting called. The Polygon collider should call it whenever another collider enters it, but for some reason it does not ever get called. Hoping someone could take a look at this and tell me what I am doing wrong.
PS. The
Picture of Code attached to Object 1
Object 1imgur.com/xRJrs.jpg
Object 2
Scene view with two colliders overlapping
Make sure all objects have are on layers that collide with each other. Edit > Project Settings> Physics2D > CollisionMatrix at the bottom
Make sure all Colliders are either Collider2D or just Collider, 2D does not collide with 3D.
Make sure the colliders are on Trigger (bool in Inspector) because you are using OnTriggerEnter
EDIT: Make sure you use OnTriggerEnter2D 2dPhysics dont interact with 3D physics. See 5.
3D physics dont interact with 2d physics
Good luck
I have a bullet game object and an enemy game object.
I want to have the ability of getting the collision point/contact, which's why I think I need the collider of the bullet to not be a trigger collider - this way, I want to have the bullet bounce off the enemy. At times, the bullet will need to bounce off of rectangular objects (not a problem), but I also want the bullet to bounce off of elliptic objects (the problem).
I want the bullet not to "physically" push the enemy, which is why, intuitively, I should set the bullet's collider to be a trigger collider.
When the bullet's collider is a trigger collider, I seemingly don't have a way to implement the bouncing.
But when the collider's not a trigger collider, it pushes the enemy object.
There's no code errors, but I think the code hasn't got anything to do with the problem.
I suspect the problem may have something to do with the Physics Collision Matrix.
EDIT
It seems that raycasting would be the solution to the problem, as Leoverload proposed. My problem became a different problem now, so I'll close off this thread, and open a new one.
Thanks for the help, #Leoverload ! :D
For the position of the hit it's very easy. You must have 2 colliders and 2 Rigidbody and then you can simply add a
Void OnTriggerEnter2D(Collision Coll) and inside it check for the tag : if(coll.compareTag("surface")) and inside you can get the position with collision.transform.position.
The collision matrix on the Physics menu only allows to remove Collision between certain layers, it's useful if you want the enemies not to push eachother!
If you don't want the player pushed you can change the collider to trigger, and destroy the bullet just after the collision with the same method as before with a void OnTriggerEnter2D and the compareTag. In this way it will look like it touches the enemy and explode, but it won't actually touch anything. If you want to add knockback to the player then you could do a simple AddForce based on the direction of the bullet and the work is done!
If you want to avoid the bullet to move walls you can set walls to static and it should work fine
So... As far as I know it's only good practice to make any GameObject Static if that object is not meant to move, therefore I also thought even a rolling stone with a rigidbody shouldn't be a static GameObject.
However, I was commented by someone as a 'Unity Tip FYI' the physics calculation spikes up when an object checks collision whilst it's rotating, so to make the collider static for the sake of better performance(even meer small amount).
I'm really not getting this idea.
Does this make any sense to anyone here??
Not exactly sure what that comment is saying. It doesn't make sense. Also, it looks like you are confusing static collider and marking GameObject as static.
Marking Object as static from the Editor
Telling Unity to batch it. It is used to increase rendering performance. (This has nothing to do with colliders)
Static Collider
Telling Unity that the Rigidbody will not be moved.
From your question, you are describing Static Collider. Static Collider work for both 3D and 2D physics system.
What is a Static Collider:
Static Collider is when you have a GameObject with any type of collider but no Rigidbody attached to it.
How to create a Static Collider:
To make a GameObject a Static Collider on, attach a collider to that GameObject but don't attach Rigidbody to it. It is now a Static Collider.
To make it non Static Collider, attach Rigidbody to it.
What it is used for:
When you have many Objects that you want your collide with but you are 100 percent sure that this Object will not be moved by you or player.
One good example is a brick wall. You simply want the wall to stop the player but you don't want to move the wall. It could also be used for stones in an environment to prevent player collison. Imagine having to attach hundreds or thousands of Rigidbodies to each stone Object with a collider.
It is used to increase physics performance.
What happens when you move a Static Collider:
Don't. When GameObject is made to be Static Collider, it is expected that you never move it. If you do, it will spike cause spike in CPU. This introduces constant freezing in your game each time you try to move it. This spike has been reduced in Unity 5 but it is still there. I think that's what that comment is trying to say. This whole thing happens because it has to recalculate the physical world all over again. From Unity's website, below is a screenshot of what happens when you try to move a Static Collider Object.
A static game object is not the same as a static rigidbody/collider, they're two completely different concept.
Static game objects are used to save performance time/memory regarding rendering, navigation meshes, etc., you can read about them here: https://docs.unity3d.com/Manual/StaticObjects.html
Static colliders are a totally different beast. A static collider is used to tell Unity that this object will never move under any circumstance, so it will save quite some calculations in the physics engine.
Finally, regarding the tip that you mentioned: I think that probably the author of that tip was talking about 3D colliders, and by "static" he meant that you should separate the 3D mesh from the 3D collider, so when the mesh must be rotated, you rotate only that and not the collider.
Think of a sphere, you create a parent game object, then two children: one that has the Mesh component, and the other the Sphere collider. When you want to rotate the sphere in place, you only rotate the mesh child and not the collider, and when you want to translate instead the sphere, you just translate the parent game object in order to change the position of both children.
so I've got an enemy in my 2.5d platformer, that I already got to follow the player around, now I want him to jump over small obstacles, and honestly, I have no idea how to. My enemy is a sphere with rigidbody and a box collider scaled 2x in X axis, effectively making it 2 times wider than the sphere. I want this box collider to be a trigger and for it to launch a AddForce.
Where do I put the code for the trigger - if I put it in void update() wouldn't it apply force until the sphere is above the obstacle? I only want it to apply the force once.
Also, what would the code for the trigger look like?
So far I only got figured out how to find the rigidbody by getcomponent and apply force to it, and I watched a video on triggers, but it didn't really help :/
Use OnTriggerEnter method like this:
void OnTriggerEnter(Collider other)
{
if (other.tag == "IMakeEnemyJumpBeforeAnObstacle")
{
// add force here
}
}
Note: this code should be in your enemy script. Plus use IMakeEnemyJumpBeforeAnObstacle tag on the trigger object which will be placed right before the obstacle. And make sure both the enemy and the other object have property IsTrigger checked from inspector.
Learn More
Hope this helps
I'm developing a game in unity using UnityScript. I've initially created two objects, a sphere and a cube, having respective colliders. Now I'm trying to detect a collision between them using the below UnityScript function. But I'm not able to detect collision. I've also added a rigid body component to both of them. How can I detect a collision?
function OnCollisionEnter(col : Collision)
{
if(col.collider.tag=="Cube")
{
Debug.Log("collision");
}
Things to verify:
Make sure the script(s) are attached to the game objects
UPDATED: Make sure the class you have this code in inherits from MonoBehaviour (C#, boo)
Make sure collider components are attached to the game objects and that "Is Trigger" isn't checked as OnCollisionEnter won't be fired if it is.
Make sure the gameobject has the "Cube" tag assigned in the inspector
Also, the rigidbody component only adds physics to the objects and is not necessary when simply detecting collisions.