hi , I have two spheres which there’s a long cube between them and each side of this cube is connected to one of these spheres. these spheres are movable . I wanted the cube between them to be elastic and be connected to these spheres and changes it’s scale and stretches when the spheres widen . is there a physic material or a joint which can do that?
Joints are intended to create some specific physics behaviours - they don’t modify the object mesh. You can achieve the effect you want with a simple script (attached to the cube):
var ballA: Transform; // drag sphereA here
var ballB: Transform; // drag sphereB here
var scale0: Vector3; // initial localScale
function Start(){
scale0 = transform.localScale;
}
function Update(){
var pA = ballA.position;
var pB = ballB.position;
transform.position = (pA+pB)/2; // place the cube in the middle of A-B
transform.LookAt(pB); // make it look to ballB position
// adjust cube length so it will have its ends at the sphere centers
var scale = scale0;
scale.z = scale0.z * Vector3.Distance(pA, pB);
// stretch it in the direction it's looking
transform.localScale = scale;
}
This works fine for a simple Unity cube, because its localScale is numerically equal to its dimensions, but may produce bizarre results for imported objects. A more general, sophisticated and complicated solution would be to use bones (take a look at Mesh) in the docs)