I have a parent object, which has four children cubes. The parent has rigidbody, no collider. The four cubes only have colliders, which trigger tags are set true. I want to handle collision in the parent object. However, it didn't work. I know use onCollisionEnter may work, but I must use trigger. Could anybody give me some ideas?
Trigger colliders are unable to use OnColliderEnter because things pass through them instead of colliding. If you wish to detect when an object enters a trigger collider, use OnTriggerEnter instead.
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
If however you wish to keep the trigger colliders and make them collide only with specific kind of objects, you can add a second set of colliders that are not triggers and use layers to differentiate which objects can collide with them and which ones pass through. You can then go to Edit->Project Settings->Physics and at the bottom at Layer Collision Matrix configure which layers can collide with each other.
https://docs.unity3d.com/Manual/LayerBasedCollision.html
Related
I had this working just fine until I decided I wanted to change how I organized the colliders just a few minutes ago to avoid having multiple colliders on the same object.
I have a "Player" object with a boxCollider2d with isTrigger=false and an empty child called "triggers" which itself has 2 children, each with a single boxCollider2d and both with isTrigger=true. These triggers are used for detecting when the player is grounded or next to a wall but are not supposed to affect the physics.
I also have a "floor" object which has a sprite and an empty child called "colliders" which has a single child called "ground" with a single boxCollider2d (isTrigger=false) and the layer of the object set to "Ground".
My understanding is that since my player has a non-trigger collider (and rigidbody) and the floor has a non-trigger collider (at least its child does), that the character shouldn't pass through the floor. Also, it's starting from a height above the ground so it's not an intersection problem.
My best guess is that somehow the child colliders of the player being triggers are turning the main collider into a trigger or that for some reason the floor collider being in a child is screwing with it, but I don't know enough about how these colliders work to really know.
The child trigger colliders should not cause it to mess up like that.
Check Project Settings -> Physics 2D -> Layer Collision Matrix. Are your objects not able to collide?
I am developing an oculus Home like project where i am facing a problem of object passes through walls and other objects. The scene is described as, Ialt texthave an object that has Box Collider and an Rigidbody (Gravity false , Kinematic true) A wall and a Floor with Box Collider and Rigidbody(Gravity false , Kinematic False) I am moving an object with hands movement. Translate object near and far using thumbstick, the problem is that when i move object far the object pass through the wall and floor. As you can see in the image below the object is passing through in the walls and the floor. Please help me and suggest a better solution. Thanks in advance.
the idea behing Kinematic objects is that interaction with Physics will not alter their transforms, so they will pass through other static or kinematic objects. To alter that you can either not use Kinematic rb's or detect collisions manually and adjust for collision results
I think you have one of your collider's Is Trigger set to true.
If the collider on your object has the Is Trigger property set to true then the collider will pass through other colliders but if it's not a trigger then it'll always collide with other non-trigger colliders. So as long as you have two non-trigger colliders they'll always collide no matter the rigid-body and if you need the Is Trigger property set to true there's a workaround you can use two types of colliders on a single object one a trigger and another a non-trigger preferably a Mesh Collider (so the trigger collider will have space to be triggered).
In my game I have 2 objects. I want them to be able to collide and not go through each other. Currently Collision is working, but when one object pushes the other, he other starts floating away. I don't want that. How can I get collisions(not going through each other) without the physics(floating away, pushing, etc)?
The component that makes the gameobject react to external forces applied on it is the Rigidbody
-You can configure constraints on your rigidbody so the passive physics (forces coming outside the object, like gravity and collisions) won't work in the axis you block. Only active forces will (like AddForce() method)
Ridigbody Component in Editor with all constraints enabled
-In static objects (like walls, a tree) you can remove the rigidbody components. It will enhance performance too. Only use rigidbody in dynamic objects like characters, vehicles, balls, bullets
-Between A and B object, at least one of them must have rigidbody, or the collision detection won't be possible (in the object which contains the method OnCollisionEnter (or Stay)
But be careful. Without the rigidbody, you won't be able to use the AddForce() method to move object. If you use simply the Translate method on the Transform, the collision detection will become a such unaccurate
I'm making a 2D Unity game, in which you can move over 3 lanes and fire arrows that only affects their specific lane.
In order to do that I'm changing the layer of the arrow prefab every time you move from one lane to another.
Arrows change to children of the terrain on hit, but when the arrow prefab change his layer, clone arrows do the same(even the ones that are parented to terrain in other lane)
How can I make arrows that are parented to terrain not to be affected by the arrow prefab change of layer?
Thanks
You say you're changing the prefab layer, if that's correct, you're basically changing every clone made from that prefab. If you want to change each lane's arrows by themselves, you should change the clones directly, not the prefab.
Maybe you could add 3 lists of arrows, one for each lane, and each time you fire an arrow, have it added to its list. Then you can iterate over that list changing every GameObject layer.
Is your arrow have a class? Add property on it like .laneNumber. Initialize the value of it when instantiating depends on the lane you're in. For example in first lane. .laneNumber = 0; And in collision method check if laneNumber is equal to the monster/obstacle laneNumber. Something like that.
I have a sphere build from multiple objects. What I want to do is when I touch/click an object, that object should find all adjunctive objects. But because none off them are moving, no collision detection can be used.
I can't find a way to detect these adjunctive objects even when the colliders do collide with each other, as I can see that in the scene. I tried all the possibilities, but none off them are working, because no objects are moving.
Is there a way to check for manual collision detection, or is there some sort of way to let Unity3d do the collision detection automatically?
You could keep a list of all those objects, then when your event happens you can send messages to all them to do what you want them to do.
Lets assume you want your sphere to break into little pieces. You send a Force message to the sphere. Then you use Newton's Laws of motion and find out how much velocity each piece gets. Remember velocity is a vector thus it has direction.
This is how I would do it and still keep the right amount of control over what happens in my game/simulation. Remember F = ma.
you could use RaycastHit (http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit.html) for your collision, this also works on non-moving objects but it needs more performance
You can add rigidbody to every objects; when you touch one of them, give a force onto it, then it is going to move and trigger event of the adjacent objects.
for the reason you do not want to move the object you touch, you can cancel movement in the OnCollider or OnTrigger event handler function.
I managed to work around this by checking the distance from the selected object and all other objects that are part of the sphere. If the distance is below a certain value, then it is an adjunctive object.
Although this is certaintly not fool proof, it works without problems so far.
I am sorry I was not clear enough. Thanks for all the advice what so ever.