Make Object Move To Mouse Position without pass through other object?

Hi every one, i am making one game like this:

  • On this, I must moving 1 object with touch dragable, when drag this object, it can not pass through other object.

  • First time, i try to use boxcollider and rigidbody, then lerp object to mouse position. Unfortunately, object draging always pass through other object.

  • So i try to create grid map, have all object in game, then when moving any object, i check current position of object with neighbor object of this, for now it’s up, down, left and right, then clamp its position with neighbor. It work! But with diagonal neighbor object, it is still pass through.

So, i decide to create this topic to ask:

How can i clamp position of object with diagonal neighbor object?

Below is video demo and 1 part of code to clamp object position of me.

Thank you very much!

Code: SampleGridClampPosition - Pastebin.com
Video: Hi every one, i am making one game like this: https://www.youtube.com/watch?v=06b0-pwuyKQ - On this, I must moving 1 object with touch dragable, when... | By Đỗ | Facebook

Hello, have anyone here?