No problem, Steve! I’ll address your questions one at a time.
1) Sharing the whole project
If you want to put the entire project up on the forum, the best thing to do is zip up the whole project folder and simply attach that to your forum post. This is preferred to a package because a package can’t include information like your Manager settings. So if you have a custom input axis set up, any scripts using that axis won’t work since it won’t exist. The forum upload limit is 50MB so if your project is bigger than that and you need OTEE to take a look, please use the Bug Reporting app and submit the project folder that way.
2) Problem with Spring Joint
The problem you were running into is basically Our Fault™. The “correct” position of an object with a spring joint is determined by the relative position of the object from its connected body WHEN the spring joint is initialized. This makes sense if you’re setting up a nunchaku or a snake made from spring joints, but not for situations like yours.
So when the Joint is initialized and the connected body is set, the current position of the sphere relative to the body is assumed to be the “correct” position. Your scenario wants the “correct” position to be the connected body’s position. The worst part is, we don’t tell this to anyone in the documentation. (I guess that would make it My Fault™)
I’ve created a bit of a workaround here, by modifying your code. Essentially, it stores the sphere’s current position, moves it to the connected body’s position, initializes the joint, and moves the sphere back where it was. This does actually work, but it’s a bit of a hack! I’ve also changed the lines that set the Anchor Point. You don’t actually want to change this for your scenario, just leave it in the dead center of your sphere. Here’s the code:
var springPower = 1.0;
var particle : GameObject;
private var spring1; // SpringJoint;
private var drawBetween; // Line Render for spring attached
function Update () {
if (Input.GetMouseButtonDown(0)) {
rigidbody.AddForce(Vector3.up * 100.0);
// Construct a ray from the current mouse coordinates
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
var connectTo : Rigidbody;
if (Physics.Raycast (ray,hit,1000)) {
// Create a particle if hit
Instantiate (particle, hit.point, transform.rotation);
connectTo = hit.rigidbody;
// Attach Spring to point of contact
var dist = Vector3.Distance(transform.position, hit.point);
// !!New lines!!
// Store the current position of the sphere
var storedPos = transform.position;
// Temporarily move the sphere to the position of the joint's connected body.
// This is because the "correct" relative position of the two objects is finalized
// when the joint is initialized.
transform.position = hit.transform.position;
// Initialize the joint
spring1 = gameObject.AddComponent("SpringJoint");
spring1.connectedBody = connectTo;
spring1.anchor = Vector3.zero;
spring1.spring = springPower;
spring1.maxDistance = 0;
spring1.minDistance = 0;
// !!New line!!
// Put the sphere back where it was
transform.position = storedPos;
//Debug.Log(dist);
// Line Render for spring vis
drawBetween = gameObject.AddComponent("DrawBetween");
drawBetween.Object1 = transform;
drawBetween.Object2 = hit.transform;
drawBetween.position2 = hit.transform.InverseTransformPoint(hit.point);
//Debug.DrawLine(transform.position, hit.point, Color.red);
}
//Debug.Log("Spring ON!");
}
if (Input.GetMouseButtonUp(0)) {
Destroy(spring1);
Destroy(drawBetween);
Debug.Log("Spring OFF!");
}
}
A more graceful solution would be to use an empty GameObject with a rigidbody that is ALWAYS the sphere’s connected body. Then as you click on different anchor points, the connected body would move to that point. This would also allow you to move the anchor point without having every anchor require a rigidbody. The problem with the Spring Joint is that it doesn’t allow you to set the “correct” distance like you were trying to do, which leads me to…
3) Creating SpringJoints from code
It sounds like you have a very good idea of how you’d like to be able to change spring length values at runtime. Please submit your idea using the Bug Reporting app. We’ll take a look and add it to the feature requests list.
Hope this helps!