I’m trying to figure out if there’s a simple way to limit movement using colliders? Basically we have a 2D game that uses a dragger to move around the items on the screen. All of that works but what we need to do is confine the movement within a bounding box (the user currently is able to move the pieces off the screen and they are lost forever). Each of the items has a box collider on it and i setup some simple box colliders on the edge but nothing seems to… collide. Is there a simple way to have the 2 interact and just stop movement or input once the colliders touch?
use onTriggerEnter script on boundaries to stop the movement.
Why not use rigidbodies on the items?
@Skyrise, i tried that and it didn’t seem to work or at least work well. Basically i put a box collider and a rigid body on my mover and outside the bounds i had a box collider w/no rigid body. That didn’t seem to block it. I then put rigid bodies on both… nada there either.
@mangoblue, i’ll look at that. I don’t have much experience with onTrigger events so i’ll see if thats something that can work.
You can use a rigidbody non-kinematic on the object you are moving and no rigidbody on the boundaries colliders. Make sure your objects’ layers can collide with each other under Edit → Project Settings → Physics.
If you’re moving kinematic object attach a rigidbody also on the boundaries collider.
Collisions in Unity are easy, but you need a bit of practice initially to understand how they work.
Ok another quick test.
Mover → Added Rigid Body and Box Collider
Blocker → Added Box Collider
When the 2 meet it kinda goes crazy and the object bounces around like its trying to stay in the bounds but can easily slip out of the bounds. Also once i let go over the mover it continues moving. it needs to be static once let go (or collided).
One more…
Mover -< rigid Body set to is Kinematic and Box Collider
Blocker → Added Box collider and Rigid Body
This works better but whenever i try and push the object through it gets caught up, goes jittery and flies off screen.
Seems like i’m missing something…
actually in looking at it this isn’t the way to go. I have other colliders that i’m hitting that i DON’T want to stop movement. I guess i’ll just hard code some bounds…
If you have some colliders that you dont want to affect with your blocker just add some tags to the gameobjects and set layer collision matrix as you like in Physics Settings. If your objects are already somewhere on the screen…are they just lying on the bottom blocker? Test if you can just drop your object from certain height and it will stop by your blocker. Are you using some code like Transform.translate on your object when user pick it up and move around?
Or i understand it wrong and you dont want to use physic?
For non physic you can also use: (68 is just example)
if (object.transform.position.x > 68) {
object.transform.position.x = 68;
}
Thanks for all the help guys… we managed to get it all up and running.
We ended up setting up a bounding box and using its extents as a transform limiter… seems a little more optimal for what we were looking for.