- Home /
Dynamically Add Points to Spline
I am trying to create a randomly generated tube. I would like the camera to fly through the tube. To do this I am using the Hermite Spline Controller, the javascript version,and when a tube piece is instantiated I take a child from the front of it, and add it to the Spline Root. The new spline points are added properly, the red line connects them at least, but for some reason the camera won't follow. If I add a few points before hitting play the camera will go to those but no further. How can I make this work properly.
One theory that I have yet to test is that maybe the problem is that the objects I am adding to the spline are cylinders v ersus using empty game objects. The camera has moved to the first point that was created before play, the rest of the line was generated at game time.
It looks like I need someway to update the current Interpolator, but I am not sure how.
My code:
public var tube_picker = new Array(0,0,0,0,1);//Ration of 0 to 1 is the percentage of Strait to Curve
public var strait : GameObject;
public var curve : GameObject;
public var instatiate_position : Vector3 = Vector3.zero;
public var instatiate_rotation : Quaternion = Quaternion.identity;
public var tube_array = new Array();
public var starting_tube_length : int = 8;
public var spline_root : GameObject;
function Start () {
//Set up the initial tube
for(var tube_piece_number : int = 0; tube_piece_number < starting_tube_length; tube_piece_number++){
var new_tube_piece = Instantiate(strait, instatiate_position, instatiate_rotation);
var new_piece_out = new_tube_piece.transform.Find("Out");
instatiate_position = new_piece_out.position;
instatiate_rotation = new_piece_out.rotation;
tube_array.Add(new_tube_piece);
//Set up next target for Spline
var new_piece_in : Transform = new_tube_piece.transform.Find("In").transform;
new_piece_in.parent = spline_root.transform;
}
}
function Update () {
if(Input.GetKeyDown("a")){//Set on button press to control the process for debugging
var picker_index = Random.Range(0, tube_picker.length); //Randomly choose between Strait and Curved
var picked_option =tube_picker[picker_index];
var picked_tube = strait;
if(picked_option == 1){
picked_tube = curve;
}
var new_tube_piece = Instantiate(picked_tube, instatiate_position, instatiate_rotation);
//Set up position and rotation for the next piece
var new_piece_out = new_tube_piece.transform.Find("Out");
instatiate_position = new_piece_out.position;
instatiate_rotation = new_piece_out.rotation;
tube_array.Add(new_tube_piece);
//Set up next target for Spline
var new_piece_in : Transform = new_tube_piece.transform.Find("In").transform;
new_piece_in.parent = spline_root.transform;
//Destory oldest piece to keep asset count low
/*var oldest_piece = tube_array[0];
tube_array.RemoveAt(0);
Destroy(oldest_piece);*/
}
}
Answer by aeroson · Jan 30, 2014 at 01:34 AM
After brief look at the code i found this
throw new System.Exception("Cannot add points after start");
So its most likely not good idea to do what you are trying to do.
Control points for the debug line are taken from transforms every OnGizmosDraw
But for the actual interpolation the transforms are only taken on Start()
Try to change line #134 of SplineController.cs from
SetupSplineInterpolator(mSplineInterp, mTransforms);
to
SetupSplineInterpolator(mSplineInterp, GetTransforms());
On first try it didn't work, but i have a question you said '#134 of SplineController.cs' but it was line 146 on SplineController.js. Were you looking at a different file or was this just a typo?
Sorry, my bad i did look at the C# version. The problem here is with the t (Time) param, lets say you have 4 control points and you have t 0.5, you are now between 2nd and 3rd control point. If you add 4 more control points you are now between 4th and 5th. So you would have to change the SetupSplineInterpolator function to not normalize the Time. Also sorry about what i suggested. You really need to understand it and fit it to your needs your self.
Thank you for the clarification, do you think this line (adjusted for js) will work, or should I just dig around the file myself? Thought I should probably dig around anyways.
After looking at the javascript version, it looks like that on each GIzmoDraw, it creates a new SplineInterpolator, but it doesn't add it to the gameObject like is done in the Start. This new interpolator is updated with the new nodes but it looks like it only draws.