setting SoftJointLimit in C# vs Unity JS

I am having a strange problem converting some Unity javascript to C#. Let me disclose that I am a kompleat C# noob (and really not very good with the js either). Basically, I am creating a ragdoll programatically, which works perfectly in javascript. But in C#, I cannot figure out why I cannot set the limits for the CharacterJoint’s SoftJointLimits.

Here is the relevant working js snippet:
Unity Javascript code:

spine.AddComponent( "CharacterJoint" );
	spine.GetComponent(CharacterJoint).connectedBody = hips.rigidbody;
	spine.GetComponent(CharacterJoint).anchor = Vector3( 0.2, 0, 0 );
	spine.GetComponent(CharacterJoint).axis = Vector3( 0, 1, 0 ); 
	spine.GetComponent(CharacterJoint).lowTwistLimit.limit = -10; 
	spine.GetComponent(CharacterJoint).highTwistLimit.limit = 30; 
	spine.GetComponent(CharacterJoint).swingAxis = Vector3( 1, 0, 0 ); 
	spine.GetComponent(CharacterJoint).swing1Limit.limit = 10.0;
	spine.GetComponent(CharacterJoint).swing2Limit.limit = 0.0;

And here is the equivalent C#, which, if I’ve understood the docs correctly, should work the same as above:
C# code:

spine.AddComponent( "CharacterJoint" );
	CharacterJoint spineJoint = (CharacterJoint)spine.GetComponent(typeof(CharacterJoint));
	spineJoint.connectedBody = hips.rigidbody;
	spineJoint.anchor = new Vector3( 0.2f, 0.0f, 0.0f );
	spineJoint.axis = new Vector3( 0.0f, 1.0f, 0.0f );
	SoftJointLimit spineLTL = spineJoint.lowTwistLimit;
	spineLTL.limit = -10.0f;
	SoftJointLimit spineHTL = spineJoint.highTwistLimit;
	spineHTL.limit = 30.0f;
	spineJoint.swingAxis = new Vector3( 1.0f, 0.0f, 0.0f );
	SoftJointLimit spineS1L = spineJoint.swing1Limit;
	spineS1L.limit = 10.0f;
	SoftJointLimit spineS2L = spineJoint.swing2Limit;
	spineS2L.limit = 0.0f;

I’ve attached two images showing the CharacterJoint settings while the app is running with the javascript and the C# script. The C# implementation seems to retain the default Twist and Swing limit values, which leads me to believe that I’m not actually setting them with the above code.

Please help a noob?

Thanks very much in advance!


The problem lies with the SoftJointLimit assignments, eg :-

SoftJointLimit spineLTL = spineJoint.lowTwistLimit; 
spineLTL.limit = -10.0f;

Making a reference like this only works when the referenced thing is an instance of a class. SoftJointLimit is actually a struct, so instead of making a reference to the original value, this code copies it and modifies the copy. You can set the values by assigning them directly:-

spineJoint.lowTwistLimit.limit = -10.0f

Also, a little tip… writing numbers like 10.0 is a JS thing - if you have the “f” at the end you don’t need to have a decimal point for the number to be recognised as a float. (It’s no problem either way but it saves a bit of typing :wink: )

And after setting these SoftJointLimit values, assign them back, too!

spineJoint.swing1Limit = spineS1L; (etc)

(I don’t think this will work

spineJoint.lowTwistLimit.limit = -10.0f

in C# because it’ll say it’s not modifiable.)

Well caught! I didn’t realise swing1Limit was a property

Thanks guys, I’ve give it a go! I had actually previously tried

spineJoint.lowTwistLimit.limit = -10.0f

but as 0turner0 pointed out, it didn’t work.

The sheer verbosity of C# overwhelms me :wink:

Thanks again!