- Home /
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.
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?
Answer by Bunny83 · Mar 25, 2018 at 12:31 AM
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:
That's a really clever solution only needing the length and calculating the handle vector automatically. Works perfect
Your answer
Follow this Question
Related Questions
localposition not working? Calculating a vector in front of an object. Picture and code included 1 Answer
Locking gameobject to rotating floor 0 Answers
Finding the forward vector of a child in local space? 3 Answers
How can I get an OverlapBox with the exact same size and position as a BoxCollider? 1 Answer
Finding the Angle Between Two Objects in 360 Degrees. 3 Answers