- Home /
How to stop object from going through walls.
I'm currently creating a base building game, so far I have object placement and grid movement on the floor, and have recently added walls. So far I've set up raycasting so objects lock towards walls instead of leaving gaps, however since i'm moving the object on a drag event and straight to the mouse position it still goes past the walls after locking. How would I stop the object I'm dragging from going through a wall, i'm currently not using any of unity's physics systems as I want to stay clear of them for a while hoping they wont be needed. I've also considered locking the axis its travelling when it snaps but the walls and objects do rotate and locking one axis wont help if the object is at 30 degrees.
Answer by Grimm9186 · Jun 25, 2018 at 04:05 PM
Think i found a solution I like, im currently using transform.worldToLocalMatrix;
to find the rotated x and y axis and when it snaps, I limit those to not continue further on, which stops it moving through walls :), thanks for all the comments, ideas and help. I'm also using a a lerp in Update to move the buildings around so you can't quickly move through the wall suggested by Thomas-Hawk
Answer by hectorux · Jun 25, 2018 at 11:51 AM
Maybe you need to lock not in localPosition to a custom Axis
Thank you. That's what I've been trying to accomplish, locking an axis based on rotation, no luck so far, anyone know how I will go about doing that?
Answer by Thomas-Hawk · Jun 25, 2018 at 12:19 PM
if you're using a raycast to determine where your built object is going to go, then you are using physics.
As long as your raycast hits the wall, and you return that in a RaycastHit variable, you can use the "point" callback to get "where the mouse is looking, but not through stuff".
You can also mix in some LayerMask to filter out what does and doesnt block the ray for example.
And then, just pass whatever the "point" is to your "instantiate" line for your built object.
Edit: Also you might want to consider an update or coroutine based way of dragging your object / making it follow the mouse.
I use empty colliders attached to each built object prefab which determine "snap positions". If they overlap, one deletes the other (unless I don't want it to), and I can still rotate my buildings before placement.
Source: Trial and error
Answer by Zodiarc · Jun 25, 2018 at 12:24 PM
I think the problem is that a raycast is checking only a single point, so the raycast can be hitting the ground but the geometry can still be in the wall. What you could do instead of just raycasting is you spawn at the hit point from the mouse position an empty with a collider with the same dimensions as the building you want to build and check if this spawned collider touches a wall.
Yes this is the problem, when the raycast hits the floor next to the wall, the objects width lets it pass through the wall. This is also an acceptable solution however then the box/building wouldn't be able to move along the wall, this is what I'm currently using till I find another solution
Your answer
Follow this Question
Related Questions
if statement not working when detecting collision between two prefabs 1 Answer
Not detecting collision? 1 Answer
Specific GameObject Collision Issue 1 Answer
Why the npc character walking strange when using a Rigidbody and Is Kinematic on enabled ? 1 Answer
Player movement ignores some collision 0 Answers