How to move an object to a predefined position

Hi there
I’ve checked many other forum entries and was checking the script reference by “grid snap” but no luck so far. The problem is simple (and the solution maybe too):
I would like to move objects on a painted 2D virtual grid. Imagine a chessboard on which you want to move objects around. I know the start pos (where the object currently is) and I know the end pos (where the object should move to). Of course the easiest way is just to put the target position by transform, but I would like to see it moving to there.
Im sure Im not the first and only one having this problem (in Javascript).
If any1 has an idea or knows the link where its described it would be great! 8)
Thanks in advance
Tyger

The easiest way to handle a grid is to set its origin at the world origin (ie, coordinate 0,0,0) and align it with the world axes. With this arrangement, you can just multiply the grid coordinate by the size of the grid square to get its world-space position.

When you say you would like to see your object move, do you mean you want it to slide directly from one square to another, or follow a path from one grid square to the next? Sliding an object is just a matter of taking the start and end positions and generating the intermediate positions over a period of time. You can use the Vector3.Lerp function to interpolate between two points. If you want the object to step from one square to the next, it will depend on the nature of the game. If the object can move through any square on the grid at any time, it is fairly easy to work out which squares are in the path. If you need the object to avoid obstacles on the way, then some kind of AI will be necessary. There are now several implementations of A* pathfinding available for Unity - this approach should be fine unless the grid is enormous and littered with obstacles. Once you have plotted a path, either by calculation or AI, then it is just a matter of moving the object between all the separate positions on the path (ie, repeating the sliding process with Vector3.Lerp for all the intermediate path points).

If you can give a bit more detail, I can probably set you up with some code to get you started on this.

Wow, a lot of input and you pointed some problems I solved so far (collitions, etc.) and yes, I have an array in background to check if any field is already occupied by another object. The collision is solved the way the object is lifted before moving, so it doesn’t touch any other object while moving to another position. Each grid field is calculated by its array x/y value, f.e. grid field 3/2 has coordinates 3x70px/2*70px : x=210px, y=140px, works all fine.
The moving operation from A to B is needed for 2 reasons:
a) The player wants to move an object onto another one (grid field already occupied)
b) The player wants to drop an object out of the array

I can check both “wrong move” situations and if they happen the object should move back to its old grid position. It will be moved quickly horizontally and vertically (both, if needed) at the same time, from f.e. grid 1/1 to grid 3/6, the most direct way over the game board, not first horizontally and then vertically.
I hope I could make the situation a bit clearer.
Thanks
Tyger

OK, as I understand it, the main thing here is making the object “hop” up in the air from one position to the other. A neat way to do this is with a Bezier curve. I’ve attached a script with a function that calculates the hop (given the start and end points and the height). As you vary the t parameter from 0 to 1, the points returned gradually trace out the path of the curve. Essentially, it works like Vector3.Lerp, but the path is a curve rather than a straight line.

195240–6958–$bezihop_197.js (647 Bytes)

Thank you for the script and sorry for delay, was abroad.
Erm, tbh I have no idea how to use this script. I have it attached to an existing object I can move around (game stone). I realized the following:

  1. The script (OnDrawGizmos) is called
  2. The script makes my current game very slow and this already by a single game stone. I’ll have up to 30 stones at the end. So I fear the game will sleep with so many game stones. I guess OnUpdate is causing this. :frowning:

I can imagine the bezier curve is looking great but will annoy the player after a while. So I really would like to move the game stone directly back to its old position, so the player can continue playing w/o delay.
Maybe I was not clear enough with my descriptiion: I am looking for a routine move a game stone in 2D to a certain position. This is used for 2 cases:

  1. The player moves and drops a game stone inside a grid. After droping I calculate the most closest grid and want to move it quickly and correctly into that grid window.
  2. The player drops a stone either outside the grid or onto an existing game stone. Both are invalid moves. So I have to qickly move back the dropped stoned back to its old position (from where the player picked up the stone).

Thats all. Now I know OnUpdate function on every stone makes the game hella slow. My game runs in kinda 2D mode, so I need it kinda that way:

function moveStone (currentPos: Vector2D, oldPos: Vector2D)
{
translate.move from currentPos to newPos the most direct way *delta.Time
}

Thats would be it.

Greetings
Tyger

Woot, you gave me the correct hint with LERP. I made it with it:

transform.position = Vector3.Lerp (transform.position, oldStonePos, Time.deltaTime*40);

The difficult part was that I have to use it in function Update() for smooth moving, but it makes no sense to move the stone all the time. Therefore I set a status variable outside the Update function. If it is 1 the Update function will do the move back to old position, else it doesn’t do anything. Hope/guess thats the correct way to do it.

Thats all I wanted, tnx!

Tyger