Hi folks. I am making a train game and I am trying to figure out the track. I wonder if someone could help me with an approach.
Right now, using a Vector3 offset, I can make 1 kind of track work (hooking up straight pieces to straight pieces for example) But the problem comes in when I mix in different sized pieces. I am trying to avoid a nasty if else chain where I say, if the previous track piece was a straight piece here is your offset, if it was a curve piece here is your offset etc.
What I would dearly love to do is make two child objects, a lock and a key on each track piece, and say to the next piece, move to the position where your lock child is aligned with the previous pieces key child.
Here is a picture that may explain this better.
Can some Unity hero point me to the right approach for this? How would I align the child of one object with the child of another?
Many thanks in advance
Something like this would help? You can get Left and Right of Straight and Curved.
using System.Collections;
using System;
class Vector3 {
public float x = 0;
public float y = 0;
public float z = 0;
public Vector3(float xx, float yy, float zz) { x=xx; y=yy; z=zz;}
}
interface Piece {
public Vector3 Left();
public Vector3 Right();
}
class Curved : Piece {
Vector3 _left = new Vector3(3, 2, 1);
Vector3 _right = new Vector3(6, 5, 4);
public Vector3 Left() { return _left; }
public Vector3 Right() { return _right; }
}
class Straight: Piece {
Vector3 _left = new Vector3(1, 2, 3);
Vector3 _right = new Vector3(4, 5, 6);
public Vector3 Left() { return _left; }
public Vector3 Right() { return _right; }
}
public class HelloWorld
{
public static void Main(string[] args)
{
ArrayList items = new ArrayList(10);
for (int i = 0; i < 10; i ++)
{
if ( i % 2 == 0 )
{
items.Add(new Straight());
}
else
{
items.Add(new Curved());
}
}
foreach(Piece item in items)
{
Console.WriteLine (item.Left().x);
}
}
}
@yoonix2 Thanks so much for your generous answer.
I wasn’t expecting an actual code example, just an approach, so I really appreciate it.