- Home /
Is there anyway to make an object impenetrable?
I'm using OnTriggerEnter on the game object the user is controlling, so the game object itself is a trigger so that it can destroy other things in its path upon collision. The problem is that I don't want the object to be able to destroy, or go through walls. I figured out how to not destroy the walls but now the game object can just go through them. Is there anyway to avoid this by either making the walls impenetrable or any other way? Thanks!
Please clarify, do you mean you don't want it to destroy, or for it to be destroyed? The way you are asking this question seems as though you are saying "I made an object be able to destroy stuff but I don't want it to destroy stuff." Please clarify this for me so I can answer your question.
I meant that I only want it to destroy some things, but not others. I'm guiding it through a maze with a breadcrumb-like trail and I want it to destroy the breadcrumbs as it moves, but I don't want it to destroy the walls of the maze. Right now it dosen't destroy the walls of the maze, but it goes through them. I want the walls to be impenetrable.
Answer by s_guy · Jun 12, 2013 at 03:30 AM
Seems like you worked out how to not destroy objects that shouldn't be destroyed. I was going to suggest making destructibility an object property. You could implement this as a tag, for example, that your destruction code tests for.
On solving object penetration: This is only well-handled automatically by moving rigid bodies and character controllers. Even then, if you use movement by translation, you'll often have colliders inter-penetrating (intersecting for one or more frames). Unity resolves this by pushing the colliders apart. The physics engine for regular collider movement by rigidbody.AddForce (or by character controller Move) does a pretty good job of keeping objects from inter-penetrating. However, it does not prevent fast moving objects from passing through thin objects if they are one one side of the wall on one frame, but on the other side of the wall the next frame.
This may be avoidable in your game by thicker walls, slower movement, and/or faster physics framerate. If not, the most precise solution then is to test for collision of the solid created by the object at its current position projected through to its position on the next frame. If this project volume collides with the wall, then you can run a more involved evaluation to determine where the object should stop.
Unity provides some built-in tools to resolve this if you're willing to simplify. You can use Physics.Raycast, Spherecast, or Capsulecast (use whichever is the simplest you can get away with or best fits the mesh of your moving object) ahead of the moving object to see where a collision will occur. See Physics. Provided you're casting (projecting) far enough, it will see any wall collision point before it would be possible to move beyond the wall by the next frame. You can then handle this collision using details from the RayHitInfo out parameter.
Thanks so much for putting so much consideration into your answer! Unfortunately, I think most of those solutions go beyond my skill-level with Unity =(
No problem. For starters, just experiment with thicker walls or slower moving objects if either make sense for your game. This can solve many problems with objects moving through your walls.
If that doesn't work, read up on examples using Physics.Raycast(). Basically, the idea is that you shoot a beam out from your object in the direction of movement and see what it hits. Since your beam can be longer than the distance your object moves from one frame to the next, you'll always spot a wall.
Thank you! I solved the problem by reworking all the code I had written, but this is really helpful for future reference!
Your answer
Follow this Question
Related Questions
How to only delete one of two collided objects? 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Destroy a specific object on trigger? 1 Answer
How do I call an on trigger enter / destroy gameObject in the scene c# 1 Answer
Why won't the coin destroy when hit? 0 Answers