Legacy Documentation: Version 5.0
Language: English
  • C#
  • JS

Script language

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

HingeJoint.spring

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Sumbission failed

For some reason your suggested change could not be submitted. Please try again in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Switch to Manual
public var spring: JointSpring;
public JointSpring spring;

Description

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

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

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

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

function Start() {
	var hinge = GetComponent.<HingeJoint>();
	
	// Make the spring reach shoot for a 70 degree angle.
	// This could be used to fire off a catapult.
	
	var spring = hinge.spring;
	spring.spring = 10;
	spring.damper = 3;
	spring.targetPosition = 70;
	hinge.spring = spring;
}
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { void Start() { HingeJoint hinge = GetComponent<HingeJoint>(); JointSpring spring = hinge.spring; spring.spring = 10; spring.damper = 3; spring.targetPosition = 70; hinge.spring = spring; } }

Modifying the spring automatically enables it.