- Home /
Moving an object which changes direction overtime and instantiates a new clone on the last position it left
As the long title implies, I have an object which will move by a speed which can be changed, one unit at a time and when it gets to a specific position, it will change direction. While the first object is moving, another object will be instantiated in the positions it left and this new object will do exatly the same movement and follow the first one. Then a third object will be created...
It is more like a snake movement. In the attachments you can find the images for the movement that I am trying to make. Square with number 1 is the first one. The others will be instantiated later and folllow the first one.
Last 2 days I have been trying with many different methods but haven't been able to find a proper way.
Answer by Kishotta · May 20, 2017 at 05:02 PM
The first thing that comes to mind would be a linked list where each "link" in your chain keeps a record of it's movement, and then relays that to the link behind it.
Say, for example, the "head" of your snake moves "up", but hasn't spawned the second link yet. It sees that it does not have a "child" link, and so it spawns one at its previous location (stored in the head). After that, the head moves to the right. The head then tells the child to move up (as the head did in the first movement), the child moves up, sees that it has no child link, and spawns one at ITS previous position (stored in the child). Rinse, repeat.
Each link only needs to keep track of it's previous movement and previous position.
I hope that makes at least some sense.
Haven't heard about linked lists before. Thank you for the guideness.