Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem adding control point after another control point via script
#1
I've searched and haven't found an answer to this. So, I start out with a curve with two points (CP000 and CP001), and a mesh path. I'm trying to add to this curve to extend the mesh path in real-time. In the interface, I can add points easily, and it just tacks on to the end in the correct location and as "CP003". The problem comes in when I try to do it in script. The documentation says "Add(CurvySplineSegment insertAfter)". I give it a segment, and it makes another CP000, and puts it in the wrong location, breaking my mesh. Here are the important bits I'm setting up:



Code:
public GameObject splineObject;
private CurvySpline spline;
private int currentPoint = 1;

//in Start()
spline = splineObject.GetComponent<CurvySpline>;();
spline.Add ( spline[currentPoint] );


I've tried adding from spline[0] and spline[2] as well, with no luck. Just using "Add()" doesn't work either. An invalid segment does nothing, and a valid segment breaks the curve/mesh every time. Please tell me if I'm misunderstanding how this works.

Bonus Question: Is there a better way to approach allowing the user to "draw" the curve mesh path in real-time? Currently I'm moving the last point along the point of interaction, then creating a new point when I get to a certain distance, repeat. Am I doing this in a weird and glitchy way?

Any help would be greatly appreciated! Thanks in advance.

 
Reply
#2
If you want to add to the end, just use CurvySpline.Add() and the segment should be added normally (that's what the editor is doing). Did you try calling MeshBuilder.Refresh() after creating the new CP?
Reply
#3
(02-11-2014, 11:21 AM)'Jake' Wrote: If you want to add to the end, just use CurvySpline.Add() and the segment should be added normally (that's what the editor is doing). Did you try calling MeshBuilder.Refresh() after creating the new CP?


 
Thanks for the response. I'm sorry to say that the answer is no, this doesn't work. As I said before, that just makes another CP000, and messes up my curve (not just the mesh). As a bonus problem, it starts throwing errors when I try to do the following:



Code:
spline.ControlPoints[1].transform.position = transform.position;

it says:

ArgumentOutOfRangeException: Argument is out of range.
Parameter name: index


Now, this error does NOT occur if I refrain from adding a point, but once I add a point (which again it names CP000, meaning the points in the curve are now CP000, CP001 and CP000 ) it starts thinking control point 1 is null.  I thought maybe I got the curve into a weird state, but I recreated the curve from scratch only to find the same result. I don't know if this is too much to go through, but the project is small and I can zip it up pretty easily if you want to check it out.

 
Reply
#4
(02-11-2014, 01:03 PM)'Jellybit' Wrote: I don't know if this is too much to go through, but the project is small and I can zip it up pretty easily if you want to check it out.
 

That's good. Could you please send it to jake<at>fluffyunderware<dot>com, I'm really curious what's going on there...

Thanks



 
Reply
#5
Sent. I'm sure I'm doing something stupid, but I've tried to be as clear as I can.
Reply
#6
Ok, I had a look. The problem is related to Unity's loading and initialization order. You didn't take that into account, so you've added the CP before the Spline initializes itself.

In general, before working with a spline from another script, do:

Code:
IEnumerator Start () {
spline = splineObject.GetComponent<CurvySpline>();

while (!spline.IsInitialized)
yield return 0; // this waits until the spline is ready

// Do whatever you want...
}
You don't need to wait for it in Start(), but then be sure to check CurvySpline.IsInitialized before working with the spline. See the docs here .

Cheers
Jake
Reply
#7
Thanks so much for your help. It worked perfectly. I weirdly assumed the curve started initialized since it looked initialized in the editor.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Rainbow Get nearest point to a world object jamesunity 6 1,143 07-16-2020, 10:46 AM
Last Post: _Aka_
  How do join 3 splines && Orientation problem athos_k 3 713 07-09-2020, 10:41 AM
Last Post: _Aka_
  Converting spline collider hit point to spline position jh092 7 1,116 06-26-2020, 09:48 PM
Last Post: _Aka_
  Control points parenting briceio 2 554 06-18-2020, 04:12 AM
Last Post: briceio

Forum Jump: