- Home /
Creating a Barrier
Currently prototyping a game I have and currently running into the issue of trying to make a barrier to not allow the player to move though. Right Now I have the player as a controllable plane, and want to make a barrier which the player cannot pass though. Is there some way of doing this. It seems so trivial but its proving a difficult task.
BTW once you generally understand the system and know what you're doing, you'll wanna look at THIS ... http://answers.unity3d.com/questions/176953/thin-objects-can-fall-through-the-ground.html
Answer by syclamoth · Nov 30, 2011 at 06:54 AM
Colliders in Unity exist entirely to interact with the rigid body-based physics system. If you are moving an object around using Transform.Translate, it won't detect any collisions, or get pushed out of any colliders unless it also has a rigidbody component with a collider on it- and even then, it wouldn't be fantastic (since that kind of movement for non-kinematic rigidbodies almost always creates problems).
For what you are doing here, you should add a 'collision' step to your movement script. In the bit where you work out how far you would move this frame, instead of moving immediately, check for if that would take your object through a collider using
if(!Physics.Raycast(transform.position, moveDirection, moveDistance))
{
// actually move
} else {
// hit a wall, do something else!
}