Snap-to function in Unity?

I am busy with a game that requires a snap-to function for the moveable tokens in the game.
As a novice coder I have been able to get the tokens to snap to position using a snap-to script tutorial. The problem is that the script is placed on the tokens and controlled by the mouse/touch. This is a difficult way of doing it, and the tokens do not stay in place if slightly bumped.
Can anyone help with advice on a simpler script that would be placed on the blue dot position, that would snap any object to it, if place on top of it as per the video clip.


  • edited
    You tried removing the collider after snapping in place and adding back when clicked on to fix it being knocked off ? Alternatively use an array of snapping points, calculate distance then snap to the nearest point so even after being moved it should snap back
  • Thanks BlueDesertDio.
    I will try the first point.
    Was just wanting to try find simpler code that could be placed on the blue dots and not on the tokens.
    The Alternative point sounds good... but not sure how to implement as a novice coder.

  • edited
    For a quick solution you can set the colliders of the tokens to not interact with each other.

    But it sounds like you are approaching this wrong by having scripts on each of the tokens, or even the goal of one on each of the dots. I would treat the whole thing as a "board" and have one script that handles the tokens and possible position they can snap in to.

  • Thanks Leslie. Yes, I agree the approach is wrong and that the second point sounds better.
    Just a difficult task for a novice coder looking for a simple solution. Guess simple solutions and coding don't do well in Unity, as I have been told many times. Have a funtastic weekend.
  • Was bored so I quickly put together an example

    Thanked by 2BlueDesertDio Gman
  • edited
    Thanks Leslie.
    That looks pretty spot on for what the game would require in terms of snap-to.
    If that is what you code quickly in your spare time, then my hat off to you.... it looks awesome. ;)
    I will study how you did this, and try to implement it and see if I can get it to work.
    Thank you once again for the time you put into this.

Sign In or Register to comment.