Dreamteck / splines

The official Dreamteck Splines for Unity repository
Apache License 2.0
22 stars 2 forks source link

ObjectController feature: Distance-based SpawnMethod #14

Open Kronoxis opened 3 months ago

Kronoxis commented 3 months ago

As requested on Discord, adds a feature to the ObjectController component that allows the controller to automatically spawn objects with a fixed distance gap.

Implementation

Adds a third Spawn Method called Distance, which when selected exposes a Spawn Distance float field. The distance field has a minimum value of 0.001f to avoid spawning infinite objects. This is implemented using the CalculateLength method, which is cached until the spline is rebuilt or the clip range changes.

Notes

The very last object will be glued to the end of the spline (accounting for clipTo), which sets the distance between the n-1-th and n-th object somewhere between 0 and Spawn Distance. If this is undesired, one can change the CeilToInt in GetTargetCount() to FloorToInt to simply not spawn the last object until there is enough room for one. We could of course add another variable to control this behaviour, if desired.

return Mathf.CeilToInt(_length / _spawnDistance) + 1;

Tests

The test branch can still be used for some basic tests. Set the SpawnMethod to Distance and play around with the spline, clip range and SpawnDistance values.


This PR should not introduce any breaking changes, as the default behaviour of the ObjectController remains unchanged. It simply exposes an optional setting for convenience.

robosoup2 commented 3 months ago

this works pretty well! However, if I move a Spline Node and then Ctrl Z, the object controller doesn't update properly & the relevant gameobjects aren't Removed properly, because of this nullref. then I have to delete all the child gameobjects and refresh the spline image In other situations, the followers will get de-synced for some other reason I haven't totally narrowed down, and updating the spawn distance will create a new line of followers without removing the old ones, WITHOUT throwing the nullref.

robosoup2 commented 3 months ago

also, I seem to occasionally get <10 fps in scene view (not even play mode) with an object controller Selected in hierarchy, even if only ~a dozen empty child objects exist. Updating the spawn distance value (*or other values) sometimes causes it, while deselecting and reselecting the object can improve it. This isn't a huge deal, but the same setup never had this issue before testing this.

robosoup2 commented 3 months ago

hmmm, also after leaving play mode it will rebuild the followers without deleting the ones already there, leading to many extra copies