I am trying to create a flying character made of segments, like a chain, that can move around in space. The segments of the chain should follow the “head” but not in a rigid line, but like a floating ribbon. I have been hacking at the camera follow script provided with unity, having each segment follow the one in front of it. It looks like this:
// The target we are following
var target : Transform;
// The distance in the x-z plane to the target
var distance = 1.0;
// How much we dampen
var rotationDamping = 3.0;
var heightDamping = 1.0;
var size = 0.8;
private var oldRotation:Quaternion;
private var targetRotation:Quaternion;
private var oldPosition:Vector3;
function Start () {
oldRotation = target.rotation;
}
function Follow () {
// Early out if we don't have a target
if (!target)
return;
targetRotation = target.rotation;
currentRotation = Quaternion.Slerp (oldRotation, targetRotation, rotationDamping * Time.deltaTime);
oldRotation = currentRotation;
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
transform.LookAt(target, target.TransformDirection(Vector3.up));
}
The follow function is called from a Fixed Update in the head. It updates the tail segments in order so they are kept in better sync.
I have 2 issues with my current implementation:
1: The segments seem to jitter a lot as the follow, particularly if the head is translating forward. I have other objects in the scene moving so it is not framerate. It happens even when I have 3-4 segments, so I am not trying to have 300 things following each other.
2: The “transform.position = target.position;” moves all of the nodes with the head, so if I flip around and move backwards they all visibly translate along before they have even caught up. I would like to not translate them with me and just pull them along when needed, like a real chain, but I am not sure how.
Thanks for any help you can provide, I am really stuck right now.