Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Precision Following
Howdy, new user here and excited to learn all about Curvy!

My first question is how to maintain precise gaps between gameobjects using the SplineController?

Ideally, I was hoping to find a "follow" component that you could just add to gameobjects that followed a lead gameobject. In the tutorial examples, I see one approach where followers are connected to the lead with joints, but those followers don't stay on the spline. In the train car example, the approach is to have every car have it's own spline controller and there's a script that adjusts the distance of follower cars in Late Update.

I'm trying to find a 3rd way that takes orientation and bend of the spline into account. For example, imagine a 2D rollercoaster with 2 cars. If I mimic the train car approach, the roller coaster cars are too far apart on the peaks, and too close together (overlapping) in the valleys. So I'm looking for help in how to adjust the distance between the 2 cars to account for the difference between peak and valley.

Hi, and welcome to the forum. I hope you will enjoy it.

As I see it, your issue happens because the controller works with positions on the spline, while your cars are outside of it (translated to the top). So even if the controllers do keep the inter-cars distance constant on the spline, the distance is no more constante after applying the translation to the top. In real roller coasters, such issue don't happen because, from my understanding, the cars height is small relatively to the tracks curvature, which is not your case.
To solve the issue, and except making the cars smaller and/or less transtaled to the top, the only solution I see is to make the inter-car distance (on the spline) variable. You will have to use some maths to know, for each car, what distance on the spline is equivalent to the desired constant distance outside of the spline (i.e: after the translation to the top), and then set the cars' positions accordingly. So you can have the first car's position set by a controller, and the remaining cars' positions set from your calculations.

I hope this helped
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Got it, and glad I'm not recreating the wheel.

Here's a simple version of a variable gap follower script for my 2d example, that works for what I need. But I could imagine a better version that uses spline references for the maths and could apply to 3D scenarios like the barrel roll spacecraft that would keep a 2nd spacecraft in a tight formation regardless of the degree of bend in the spline.


Hope this helps somebody.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using FluffyUnderware.Curvy.Controllers;

using UnityEngine.UI;

public class splineFollower : MonoBehaviour {

public Text debugBox;

public SplineController leadCar;
private SplineController followCar;

public float followGap;
public float valleyGap;
public float peakGap;

private float cGap;

void Awake() {
followCar = this.GetComponent<SplineController>();

// Update is called once per frame
void LateUpdate () {

float bendAngle = Vector3.SignedAngle(leadCar.transform.up.normalized, followCar.transform.up.normalized, Vector3.forward);

if (bendAngle < 0f) {
debugBox.text = "inner bend = " + bendAngle.ToString();
//increase gap
cGap = Mathf.Lerp(followGap, valleyGap, -bendAngle/100f);  
else if (bendAngle >= 0f) {
debugBox.text = "outer bend = " + bendAngle.ToString();
//decrease gap
cGap = Mathf.Lerp(followGap, peakGap, bendAngle/100f);  

followCar.AbsolutePosition = leadCar.AbsolutePosition - cGap;
Thanks for sharing your solution
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.

Forum Jump: