07-18-2018, 12:09 AM
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.
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.
Code:
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;
}
}