- Home /
How to add friction to Unity HingeJoint?
I attach a rigidbody to a HingeJoint in Unity.
It's attached no at the the center of an anchor of a joint.
To stop it from falling I set Use Motor = true
, Target Velocity = 0
and apply some force to the motor.
However, this does not help, see video:
http://g.recordit.co/JJ0cmfT1Mb.gif
Here is the sample project: https://drive.google.com/open?id=0B8QGeF3SuAgTNnozX05wTVJGZHc
Screen from sample project to reproduce the issue (there is no code):
How can I apply a friction to a joint to stop rigidbody from moving?
Note: setting spring with damper or applying a limit is not an option for me, because I need to rotate the rigidbody correctly when I apply enough torque (force) to a motor.
If I switch damper/limit on and off, the rigidbody will move faster when it is rotated in a direction of falling and slower when it is rotated in opposite direction.
Answer by ZiadJ · Feb 22, 2018 at 05:19 PM
I found a similar question here and a seemingly correct way to achieve it: https://stackoverflow.com/questions/44497538/how-to-add-friction-to-unity-hingejoint
using UnityEngine;
public class JointFriction : MonoBehaviour
{
[Tooltip("mulitiplier for the angular velocity for the torque to apply.")]
public float Friction = 0.4f;
private HingeJoint _hinge;
private Rigidbody _thisBody;
private Rigidbody _connectedBody;
private Vector3 _axis; //local space
// Use this for initialization
void Start()
{
_hinge = GetComponent<HingeJoint>();
_connectedBody = _hinge.connectedBody;
_axis = _hinge.axis;
_thisBody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void FixedUpdate()
{
var angularV = _hinge.velocity;
//Debug.Log("angularV " + angularV);
var worldAxis = transform.TransformVector(_axis);
var worldTorque = Friction * angularV * worldAxis;
_thisBody.AddTorque(-worldTorque);
_connectedBody.AddTorque(worldTorque);
}
}
Your answer
Follow this Question
Related Questions
Hinge Joint Giving Fake 'Slack' 0 Answers
Hinge joints not reaching target position 1 Answer
Disable weight "inheritance" when using HingeJoint2D? 1 Answer
How can I make a configurable joint behave like a spring joint? 0 Answers
Violent shaking when using position drive in Configurable Joints 1 Answer