Legacy Documentation: Version 5.1
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

HingeJoint.spring

Switch to Manual
public var spring: JointSpring;

Description

The spring attempts to reach a target angle by adding spring and damping forces.

The JointSpring.spring force attempts to reach the target angle. A larger value makes the spring reach the target position faster.

The JointSpring.damper force dampens the angular velocity. A larger value makes the spring reach the goal slower.

The spring reaches for the JointSpring.targetPosition angle in degrees relative to the rest angle. The rest angle between the bodies is always zero at the beginning of the simulation.

See Also: useSpring, JointSpring.

#pragma strict
public class HingeExample extends MonoBehaviour {
	function Start() {
		var hinge: HingeJoint = GetComponent.<HingeJoint>();
		// This could be used to fire off a catapult.
		var hingeSpring: JointSpring = hinge.spring;
		hingeSpring.spring = 10;
		hingeSpring.damper = 3;
		hingeSpring.targetPosition = 70;
		hinge.spring = hingeSpring;
		hinge.useSpring = true;
	}
}

Modifying the spring does not automatically enable it.

Enabling the motor overrides the spring, given the spring was enabled. If the motor is again disabled the spring will be restored.