Sliding object around a grid

So I’m trying to create this “slider puzzle” for a vr game wherein the player manipulates a pair of objects and slides them around a grid. (See picture below). The way I’ve got it working now is when the object hits a junction point, I use the velocity vector of the player’s hand that is currently interacting with the slider and depending on if the magnitude of the horizontal or vertical component is greater than the other, I constrain movement of the object to that axis whose value is greater until I’ve moved the ball (using a basic set location and grabbing the horizontal/vertical component of the world location of the player’s hand) and reached another junction point. This method is causing me some issues as it doesn’t always choose the axis that I want to move along and also, I wanted to add in some diagonal paths, which this method doesn’t allow. So, my question to you all is how I can do this a better way? My first iteration actually involved using a spline and constraining the movement of the slider object on the spline itself but the problem was that I couldn’t make a multi-directional path this way. Perhaps there’s a way to make a grid using multiple splines? Let me know what you guys think.

Have you tried using SnapToGrid + locking an axis?

I’m assuming you need this movement in real time, rather than drawing object path ahead of time (which would be much easier) and then making the object follow. This is untested but here’s how I would approach it:

  • no preexisting grid is needed
  • you lock the movement axis using the velocity vector of the player’s hand
  • have a helper object snap to grid once you know the direction
  • every time a snap is done, create a point - add it to an array or add it to a spline (if you want use that spline for something else later on)
  • at this point you’ve got 2 points you can lerp between them
  • set it in such a way that the movement is done between between the Last -1 & Last points so if the user decides to go forth and back between 2 points only, it would still work

Not seeing any major snags with this but there surely is a gotchya! I did not think about.