Hi there,
I have set up the following code to place waypoints along the track in my game. It’s all working fine, waypoints are properly positioned and rotated but when I try to apply an offset to the local x position, it doesn’t work. My road is perfectly even, and when the offset is applied, each waypoint should be in the middle of the road, but it seems to be adjusting it to global space. I tried making the waypoints children but that made it worse. It’s probably something really simple but any help would be greatly appreciated.
#pragma strict
var fOffset : float; // Use to adjust how far from edge of road waypoint is to be set
function Start ()
{
var roadMesh : Mesh = GetComponent(MeshFilter).mesh;
var vertices : Vector3[] = roadMesh.vertices;
var iWaypointGap : int = 8; // Use to set waypoint every ??? vertices
for (var i = 0; i < (vertices.Length) - iWaypointGap; i += iWaypointGap) // Depending on road mesh, may need to be reverse for loop
{
// Add waypoint
var wayPoint = new GameObject(); // Create waypoint
var newPos = transform.TransformPoint(vertices*); // Set to vertex's position in global space*
-
var newPos2 = transform.TransformPoint(vertices[i + iWaypointGap]); // Set to next vertex's position in global space*
-
wayPoint.transform.position = newPos; // Set waypoint to vertext position*
-
wayPoint.transform.LookAt(newPos2); // Set waypoint rotation*
-
//wayPoint.transform.localPosition.x -= fOffset;*
-
wayPoint.name = "Waypoint_" + i / iWaypointGap;*
-
}*
}
What about saving the localPosition before modification ?
I don’t know for JS but in c# you have to store position / scale values of a Transform before modification and set them as a whole Vector3. Trying to change only a value of a Transform Vector3 pushes an error.
Try something like this:
var localPos = wayPoint.transform.localPosition;
localPos.x -= fOffset;
wayPoint.transform.localPosition = localPos;
I hope it helps.
I’m not too sure why it wasn’t working originally but transform.right fixed it. Here’s the code, for anyone else who might be interested:
#pragma strict
var fOffset : float; // Use to adjust how far from middle of road waypoint is to be set
var waypointContainer : GameObject;
function Start ()
{
var roadMesh : Mesh = GetComponent(MeshFilter).mesh;
var roadVerts : Vector3[] = roadMesh.vertices; // Array of mesh vertices
var iWaypointGap : int = 8; // Use to set waypoint every "iWaypointGap" vertices
var fRoadWidth = Vector3.Distance(roadVerts[0], roadVerts[2]); // Get the distance between vertex 0 & 2
for (var i = 0; i < (roadVerts.Length) - iWaypointGap; i += iWaypointGap) // Depending on road mesh, may need to
// iterate through the for loop backwards
{
var waypoint = new GameObject(); // Create waypoint
waypoint.transform.parent = waypointContainer.transform; // Put into container to keep tidy in hierarchy
var vertPos = transform.TransformPoint(roadVerts*); // Current vertex's position in global space*
-
var nextPos = transform.TransformPoint(roadVerts[i + iWaypointGap]); // Target vertex's position in global space*
-
waypoint.transform.position = vertPos; // Set waypoint position to current vertex position*
-
waypoint.transform.LookAt(nextPos); // Set waypoint rotation*
_ var centrePos = waypoint.transform.position + (fRoadWidth / 2) * waypoint.transform.right; // Centre of road position_