Class member Vector3 based on local transform

Background:

I’m making prefabs for models that represent different parts of a street (straight, curved, 3 way intersection, 4 way intersection). With this I’m also including nodes on how cars can drive on the tile. Right now I’ve switched from manually placing a bunch of nodes to simulate a curve and instead defining tangent handles. Each node keeps a list of its neighbors that it can go to, and this “Neighbor” class contains the point, distance and tangent handles.


public class Neighbor
    {
        public WayPoint point;
        public float distance;
        public Vector3 handleStart;
        public Vector3 handleEnd;

        public Neighbor(WayPoint point) : this(point, point.transform.position, point.transform.position)
        {
        }

        public Neighbor(WayPoint point, Vector3 handleStart, Vector3 handleEnd)
        {
            this.point = point;
            this.distance = 0;
            this.handleStart = handleStart;
            this.handleEnd = handleEnd;
        }

        public void UpdateDistance(Vector3 from)
        {
            this.distance = Vector3.Distance(this.point.transform.position, from);
        }
    }

The problem:

My issue is that when making the prefab I’m entering the Vector3 positions of each handle, but then upon rotating the prefab its not also rotating where the handle should be so my curves are getting all messed up.


0 Degree Rotation (correct)

90 Degree Rotation (incorrect)


Question:

My thinking is that I may have to check the rotation of the prefab and figure out some calculation to get the proper rotation, but at the same time I feel like there has to be something Unity already has to do this (since it does it for other things such as the node gameobjects). Another option is I could create gameobjects for each handle but this seems excessive. Is there anyway to tell unity that a Vector3 is relative to the gameobject and not the world space?

You define your “handles” / tangents in localspace of a tile. To get the worldspace direction you just use transform.TransformDirection() to convert the local space direction into a worldspace direction.

However since you already use gameobjects as nodes you may want to simply rotate the nodes so the nodes forward vector represents the desired direction. You may want to always make the forward vector points along the travel direction. So the start tangent would point “inwards” while the end node would point “outwards” (inward the next tile). So when connecting the tiles the two nodes at the connection would have the same direction. This way you don’t need to manually specify a vector but only need to specify a “length” of the tangent to get the correct curve. That means the start tangent would be simply

transform.forward * neighbor.startLength;

and the end tangent would be

-neighbor.point.transform.forward * neighbor.endLength;

Note the minus since for the curve we need the tangent into the other direction.

Here’s an image of what i meant:

113635-4wayintersection.png