If I spawn in new Rope Parts, they get hooked to the next joint, but they rotation is completely off. So the Rope sticks together, but doesn’t look like a rope anymore.
public class RopeManager : MonoBehaviour
{
public RopePart[] ropeParts;
public RopePart ropePrefab;
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab)){
AddRopePart();
}
}
public void AddRopePart() {
HingeJoint2D ropeMiddleJoint = ropeParts[(int)ropeParts.Length/2].GetComponent<HingeJoint2D>();
RopePart newRopePart = Instantiate(ropePrefab,ropeMiddleJoint.GetComponent<RopePart>().NextSpawnPoint.position, Quaternion.identity,gameObject.transform);
HingeJoint2D newHinge = newRopePart.GetComponent<HingeJoint2D>();
newHinge.connectedBody = ropeMiddleJoint.connectedBody;
ropeMiddleJoint.connectedBody = newRopePart.GetComponent<Rigidbody2D>();
}
}
How to spawn the RopeParts with the right rotation? Do I missing something here?
I actually remember trying something like this myself and hitting this exact issue.
It felt like something about the joint’s velocities reset or something when you add / remove segments.
I wonder if it would work better by simply keeping all the joints in place in a line ( making them all at once first for instance) but calling SetActive() to cull the end pieces…
Paging @MelvMay once again sir… interesting rope change length question…
Most likely it’s that the joints are being created way out of constraint. When you start the simulation, everything has to somehow figure out what the stable set-up and get there. You should try to create things that are inside or very close to the constraint.
TBH, I’m not following that code at all and with all the “GetComponent” calls it’s even more confusing. I don’t see anchors being set either, hinges connect at anchor points so just connecting them together means they all connect to the same point. It just looks plain wrong TBH.
Even when you correctly set-up a chain of joints, you should always ensure you increase the solver iterations to keep it stable and use a max-length distance joint to stop stretching.
If I understand him correctly the spawn position doesn’t matter, as soon as the joint is hooked up, it will go to his right world-space position all by himself.
I know its bad code right know.
But it’s working as a first Prototyp.
I needed to set the connectedAnchor to fix Vector2 to make this work
My Code for any folks who might run into this in the future
public List<RopePart> ropePartsList;
public RopePart ropePrefab;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab)){
AddRopePart();
}
}
public void AddRopePart()
{
// Spawns the new Rope Part
int ropeNum = Mathf.RoundToInt(ropePartsList.Count/2);
RopePart ropeMiddle = ropePartsList[ropeNum];
HingeJoint2D ropeMiddleJoint = ropeMiddle.GetComponent<HingeJoint2D>();
RopePart newRopePart = Instantiate(ropePrefab,ropeMiddle.NextSpawnPoint.position, Quaternion.identity,gameObject.transform);
HingeJoint2D newHinge = newRopePart.GetComponent<HingeJoint2D>();
Debug.Log(ropeMiddle.name);
Debug.Log(ropeMiddle.NextSpawnPoint.position);
//This is the imported paragraph
//Connected the Joint
newHinge.connectedAnchor = new Vector2(0.49f,0);
newHinge.connectedBody = ropeMiddleJoint.connectedBody;
ropeMiddleJoint.connectedBody = newRopePart.GetComponent<Rigidbody2D>();
Debug.Log(newHinge.connectedAnchor );
//Add new Part to List
ropePartsList.Insert(ropeNum, newRopePart);
LineController.Instance.AddPointToList(ropeNum, newRopePart.transform);
}