- Home /
Unity minecart 2d idea
Hello everyone. I am new to Unity and I wonder if someone could help me with my game 2D any idea how to do. The idea is to make the cart can walk between independent rails as in this video: https://www.youtube.com/watch?v=Gt1qf2SrkME
I've heard in ITween but I can only make it stand following a path
Answer by robertbu · Aug 07, 2014 at 04:16 PM
Let's assume iTween, because if we open it up, this question has a lot of possible implementations and becomes a design question better posted on Unity Forums.
Define two parallel paths in iTween. There is a free visual path editor for iTween you can get here, but you can also define your own array of Vector3s or array of Transforms.
Use iTween.PutOnPath() and iTween.PointOnPath() for placing and moving objects on the path (not the orienttopath for things like MoveTo()).
PutOnPath() and PointOnPath() use a fraction between 0.0 and 1.0 where 0.0 is the beginning of the path and 1.0 is the end of the path. If you get the current position and then a position slightly ahead of the current position, you can produce and direction and from that an angle. This gives you the needed information to angle your cart and to calculate the physics (go faster downhill and slower uphill).
To switch paths, use the same fraction but replace the array used. Here is an answer for someone doing that with iTween.
In the link just provided, you will also see code that increments the fraction from 0.0 to 1.0 and therefore move the object along the path.
The physics of swapping I'd handle by moving an empty game object along the path with the visible object a child. When switching paths, the empty game object moves immediately, but the physical game object lags and is Lerp()ed or MovedToward()ed using Trandform.localPosition. Here is a similar question to give you an idea.
I understand the idea and am very grateful for your help. I'm following this example but I have a doubt. During the jump the car can find the current path or another path. How can I do understand that the car jumped an path and found another path?
Using PutOnPath(), your position is specified by a fraction. So if you specified 0.5 as the fraction, the object would be at the 1/2 point on the path. As I mentioned, I would structure this so that an empty game object is walking along the path with the visible object as a child. When the user changes to another path, the empty game object immediately changes to the new path at the same fraction, but by modifying the the localPositon in the visible child, the visible object initially doesn't change position but is Lerp()ed to the same position as the empty game object.
Imagine the visible object connected to the empty game object by a spring. When the cart switches paths, the empty game object is immediately placed on the new path stretching out the spring. The visible object catches up with the empty game object as the spring contracts. There is no finding involved.
There is one issue with the way iTween handles paths that may require you to be extra careful. You want a one-to-one mapping of positions between the two paths, in terms of number of vertices, and you want the 'x' position of each vertex to match up as well. So if you need to curve the upper path up for a jump, you might place 'unnecessary' vertices in the lower path to keep things matched up. This would not be true if you went with a more sophisticated spline package as are available (for $) in the Asset Store.
Sorry my little understanding of Unity. I managed to make an example of the first link. I made 3 paths with iTweenPath and they change paths by pressing the buttons! But I want to make irregular paths , broken roads, making the cart "jump" between the span or jump to another path
I thought about putting in some places the polygoncollider In These three paths. The cart will run on These colliders and- When not collide, the card will fall. When you jump and find the collider, will go que way. Do it this way is right?
Physics and iTween are very different approaches. And there is an a middle choice of using colliders and a character controller of some sort. Note I was looking specifically at what they do in the video. It would be easy to extend the path solution so that there were breaks in the path (and more paths), but you would not have random platforms like are in your drawing. This is devolving into a design question. $$anonymous$$aybe you want to get a real clear vision of what you want (and how it is different than the video), and then ask a design question on Unity Forums.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Unity Sprites Overlapping, slightly see-through (2D game) 2 Answers
How do I use MoveTowards in 2d? 1 Answer
How to update assigned vector variable in unity C# 1 Answer
Creating 2D art in a 3D world 3 Answers