Follow Script for Chain

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.

Also, I have been looking at trying to do it with Joints, but I am not sure how to use them for this setup. Using the spring joint just pulls the head backwards and doesn’t move the body along.

You could use the configurable joint component for this. Create a set of objects for the head and tail segments and add a rigidbody to each. Then, attach a configurable joint to each tail segment. Set the joint’s connected body to be the segment in front, then set the XYZ motion parameters to Locked and set the anchor point to <0,0,1>. (Actually, the easiest way to make a chain of joints is usually to get the first one set up correctly and then make duplicates.) You may need to tinker with the settings quite a bit to get the exact behaviour you want but you should find the results are quite good even though there is very little coding involved.

Ill try that out. I attempted a config joint with all directions locked but that forms a rigid chain behind the head. The spring and dampen functions seem right, but would those impact angles as well? As soon as I switch to limit, even a limit of 1, the pieces start to act very strangely, going into an orbit around each other. Any suggested starting values? The docs have definitionsbut lack real examples of working settings.

It’s probably best if you set the XYZ motion parameters to Locked but set the XYZ angular motion to limited (you can lock one or more of them if you don’t want the joint to move in that axis, of course). You can apply spring and damping to the rotation if you set the angular XYZ limits. The configurable joint often needs a bit of experimentation to get its behaviour exactly right for a given game.