I created a cord for a toaster using little capsules that are connected end to end with CharacterJoint/RigidBody. I use a script to tie the pieces together. The rope physics work great unless we get to higher speed. And in my game the jumped up toaster is supposed to hit the player in the face. The force I need to apply to get it to reach, causes it to fly forward so fast, that the rope “shatters”, as seen below.
Here are the character joint parameters:
And the code that assembles it is here:
public void Spawn()
{
this.CordHolder = new GameObject("Cord Holder");
if (EndAttachment)
{
this.Root.transform.root.position = this.EndAttachment.position + Vector3.up * this.Length;
}
int count = (int)(this.Length / this.SegmentDistance);
var firstSegment = Instantiate<GameObject>(
this.CordPrefab,
transform.position + new Vector3(0, -this.SegmentDistance, 0),
this.CordPrefab.transform.rotation,
this.CordHolder.transform);
firstSegment.name = "Segment0";
firstSegment.GetComponent<CharacterJoint>().connectedBody = GetComponent<Rigidbody>();
Rigidbody lastBody = firstSegment.GetComponent<Rigidbody>();
if (SnapFirst)
{
lastBody.constraints = RigidbodyConstraints.FreezeAll;
}
for (int i = 1; i < count; i++)
{
var segment = Instantiate<GameObject>(
this.CordPrefab,
transform.position + new Vector3(0, -this.SegmentDistance * (i + 1), 0),
this.CordPrefab.transform.rotation,
this.CordHolder.transform);
segment.name = "Segment" + i;
var joint = segment.GetComponent<CharacterJoint>();
{
joint.connectedBody = lastBody;
}
lastBody = segment.GetComponent<Rigidbody>();
}
if (SnapLast)
{
lastBody.constraints = RigidbodyConstraints.FreezeAll;
}
if (this.EndAttachment)
{
lastBody.constraints = RigidbodyConstraints.FreezeAll;
lastBody.position = this.EndAttachment.position;
lastBody.transform.SetParent(this.EndAttachment);
}
}
I’m pretty sure that as one piece moves too fast, that the next piece flies with it on the next frame, and then each frame another one moves, or they all move a little together, but they pull hard, and cause all this flailing about. I want to keep the cord in one piece and keep it looking like a cord, not a bunch of flies buzzing around. Any idea what I’m doing wrong, or how to improve the rope physics?
Thanks.