- Home /
The question is answered, right answer was accepted
Is there a Distance Constraint 3D or Distance Joint 3D in Unity?
Is it possible to do a distance constraint/joint constraint in Unity? For example, consider 2 cubes, 2 unit apart, as the following;
[ ]--2 units--[ ] (controllable)
If we move the box on the right up or down (by using keyboard control), it will retain same distance, 2 units exactly, as the following;
[ ] (controllable)
/
2 units
/
[ ]
It seems that we have Distance Constraint 2D, but is there a Distance Constraint 3D in Unity?
Answer by Dinosaurs · Jan 08, 2014 at 11:59 PM
I believe the best way to do this would be using a ConfigurableJoint and setting it's linear limit to the distance you want.
Answer by darkcookie · Jan 09, 2014 at 12:19 AM
well i would get the distance of 1 and compare it to the other ... from there you can set a constraint of distance to your likeing.
var target : Transform;
private var myTransform : Transform;
function Awake(){
myTransform = transform; //here i cached the transform for performance
}
function Distance(){
var direction : Vector3 = target.position - myTransform.position;//declare direction
if (direction.x > 2){
//Your code here if the distance on the x is GREATER than 2
}
else if (direction.x < 2){
//Your code here if the distance on the x is LESS than 2
}
}
or you can do a ray cast and measure the distance apart
I'm not quite following your code. function Distance(){...}
should be inside function Update() {...}
isn't? Why do you use direction.x
only? Thanks.
the distance is in a function and not in the update because you can call it whenever you want (if you want you can put it in the update function) its just for better control and performance , also i use x as an example ....you pick your vector and you can use this as a condition for the ConfigurableJoint like Dinosaur said.