- Home /
making elastic object
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?
thanks
Answer by aldonaletto · Jun 16, 2012 at 04:15 PM
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)
thanks os much ,I think this is what I should do , but the other problem is how can I make the edges of the cube fully connected to the spheres . cause if the rotation of the cube changes , the edges will not be fully connected to the spheres and it will look a little bit unrealistic
If the cube has X and Y scales set to 70% or less than the sphere diameter (scale = (0.7,0.7,1.0), for instance) its corners will be hidden inside the spheres, and you will see a cubic bone linking them. If the X and Y scales are bigger than 0.707, the corners will appear no matter what you try.
Can you please suggest how to achieve this using bones? I am trying to create a 2d slingshot effect.
Separating bones manually will usually cause a mesh to stretch. I suppose this can help you achieve what you want.
Your question was rejected as it was a general design question, not a Unity specific one.
Answer by ExTheSea · Jun 16, 2012 at 04:10 PM
You have to make a physics material and try a friction around 1.
Got it from here:
http://answers.unity3d.com/questions/58794/getting-realistic-bounces-using-physics-materials.html