- Home /
keeping constant "Distance Joint 2d"
I am using the "distance Joint 2d" component for dragging an object. it seems that although the "Max distance Only" check box is selected if moving fast the parent object , the child object distance grows to be more than defined in the "Distance" param.
why?
//Parent movement code
using UnityEngine; using System.Collections;
public class moveDude1 : MonoBehaviour { Vector2 current_position; // Use this for initialization void Start () { current_position = transform.position; }
// Update is called once per frame
void Update () {
if (Input.GetKey ("right")) {
//transform.Translate(1,0,0);
transform.position = new Vector3 (transform.position.x + 1, current_position.y, transform.position.z);
} else if (Input.GetKey ("left")) {
//transform.Translate(-1,0,0);
transform.position = new Vector3 (transform.position.x - 1, current_position.y, transform.position.z);
//} else {
// transform.position = new Vector3 (transform.position.x, current_position.y, transform.position.z);
}
}
}
Answer by shaibam · Jul 05, 2015 at 01:33 PM
Before you answered I resolved to working with a 2 hinge joints instead of the DistanceJoint. It seems to do the job, though it might not be intended for it.
anyway I've later tried your solution, and since I didn't want to touch the project settings. your suggestion for applying force instead of the transform method really helped.
thank you very much
Answer by KevinCodes4Food · Jul 02, 2015 at 06:35 PM
I implement a joint "chain" in a pickup truck game, and had similar issues with joints moving beyond set bounds, or even breaking when set to infinite break strength.
I used three solutions to keep joints within assigned attachment space.
First, I increased the physics time step by decreasing the Project Settings -> Time -> Fixed Timestep. For my game, 50 to 100 updates per second worked well (0.02 to 0.01). But be sure to watch for performance issues if set too high.
I increased the physics Solver Interation Count in Project Settings -> Physics. Around 10 seemed to work well, but if you have lots of collisions, springs, joints, etc. this can create performance issues.
I made sure rigidbody objects attached to joints were only updated with force or rigidbody updates. Instead of setting the position directly in the transform as you are in your code, try applying a force or using Rigidbody.MovePosition
Hope that helps!
Kevin