Simple Collision Detection- Blocking Movement?

I have looked through the reference, tutorials, forums, and ask Unity, but can’t find this simple question asked. It’s difficult to search google for this as everything BUT the question pops up, as others seem to use prefabs.

I have Box collidors on my objects and player, and a rigidbody on my player.

Now… I am trying to find the code to block movement so the player can’t walk through objects with box collidors, can’t walk through walls, can’t walk through objects, etc.

Yet I can’t find anything here. I am afraid of messing with movement, as my mind comes up with only resolutions which would cause my player to freeze and have his speed set to 0, becoming paralyzed from the neck down.

Simple question, but certainly not something easily found in the reference, forum, or ask Unity website. Ugh!

There is no code needed to make an object have collision. Instead of adding a box collider to the object, add a mesh collider. See if that works.

weird… if you have attached a rigidbody, then it must to work…
take in mind you must to have disable “isKinematic” to detect collisions…

finally, can you share a little example project to test it…

greetings.

for a character generally capsule setup work best shape wise , plus mesh collider in animated character wont work at all , so this is somthing to keep in mind

otherwise the character controller provided by unity offer a nice and smooth blocking collisions generally.

but as InvaderStudio said basically get collision to take effect in unity doesn’t require code right away, adn should work out of teh box if you setup your stuff correctly in the scene

I attached rigidbodies to everything and made sure the Z layer is the same, made sure to uncheck isKinematic (and checked, I tried every combination lol)

Wall = Box Collider + Rigidbody
Player = Capsule Collider + Rigidbody

Still walking through walls!
I actually attempted to make a brand new project. I did the following.

  1. Created a Cube.
  2. Box Collider “Is Trigger” box checked.
  3. Added a Rigidbody
  4. Turned off Gravity
  5. Kept IsKinematic off (I also tried it on, on both, on only one, etc.) but off.
  6. Duplicated the Cube
  7. Added X axis movement where it only moves on the X axis.
  8. Walked Cube1 through Cube2.

Try unchecking “Is Trigger”.

Winning.

My game is a 2D game, so I locked everything except X and Y position, but I still have a problem. Whenever it collides, it “bounces” off of the object and starts flying backwards to infinity.

Honestly, I just want a simple block movement, can’t go through walls, type of thing. Not a “You collided, so now you will go backwards unto infinity in space like Bender in Futurama.”

in that case you can use just trigger and isKinematic , and from the event you get from your wall trigger let say , just define the position which should stay the player.

or something in this idea, after this come on how complex your wall are setup , if you are using true 3d objects but in a 2d fashion view then try using character controller eventually for your main character, it generally do the work ( may be just a bit expansive perf wise , but for a unique entity in game this still stay quite reasonnable ).

if you are using sprite then either try your way around trigger or otherwise you will need to come with a more “custom” solution.

Yes, I am using a sprite on a plane.

So you’re saying I’ll have to code my own system after all?

Oi vei!

Does anyone have a solution to this via possible prefab or script?

Or perhaps somehow don’t have the “bounce back” aspect to the collision?

what method you apply to make it move ?
if you are using rigidbody.addForce… increase the “drag” of the object might help.

var hToMove = Input.GetAxisRaw("Horizontal");
var vToMove = Input.GetAxisRaw("Vertical");

//Move Player
transform.Translate(Vector3.right * hToMove * hPlayerSpeed * Time.deltaTime);
transform.Translate(Vector3.up * vToMove * vPlayerSpeed * Time.deltaTime);

Does anyone have any experience with pure 2D games on planes, and collision detection?

The code I have disables momvement, so that whenever he bumps into a wall, he becomes paralyzed.

I can’t seem to figure out the code to make him able to still move, just not in the direction of the wall’s collidor.

Unchecking isKinematic makes him bounce off the walls and start flying away for infinity, which is strange as well.

Thank you for your help.

Don’t know if it’s too late to reply.
in your case, you might think of using 3d objects instead of 2d plane.

I would also like to find a solution to this. I’m using a Character Controller with my main character, and I’ve used a the same for an object and it passes right through it. If I use a box collidor, it kicks the box around (it collides and works, but the box isnt meant to move, but to PREVENT the character from moving).

I have rigibodies on everything, assuming it couldn’t hurt.

Don’t know what the OP’s problem was. Mine worked without the need for a Character Controller.

Capsule Collidor on Player + Rigidbody.
Box Collidor on Wall.

Player cannot move through wall. Works perfect right out of the box.

You only need one player with any collider and a rigid body. lock the movement. You only need one cube with collider, not rigidbody. You don’t need any other parameters or anything at all.

You are probably over-coding it and messing it up.

i’m having some issues getting this working. I’ve got 2 objects (a simple cube w/a box collider on it + rigid body) and a blocker which is a game object with a large box collider on it. No matter what settings i change the cube will go right through the collider object, no problem. I’m trying to create a blocker system for a mini game so the user can not push the pieces beyond a point and so far nothing has worked for me. :frowning: