- Home /
Springjoint transmit wrong forces
Hello!
We have a problem with physic here ^^

So I have a rectangular plateform (perfectly flat) that has a boxCollider (not trigger) and a rigidody. I have above it 3 little cubes with boxCollider (not trigger) and a rigidody (with gravity).
I use the script founded in the community :
var spring = 50.0;
var damper = 5.0;
var drag = 10.0;
var angularDrag = 5.0;
var distance = 0.2;
var attachToCenterOfMass = false;
var hit : RaycastHit ;
var platform : GameObject ;
var go : GameObject ;
private var springJoint : SpringJoint;
function FixedUpdate ()
{
// Make sure the user pressed the mouse down
if (!Input.GetMouseButtonDown (0))
return;
var mainCamera = FindCamera();
// We need to actually hit an object
if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit, 100, 1<<LayerMask.NameToLayer("TouchableObject")))
return;
// We need to hit a rigidbody that is not kinematic
if (!hit.rigidbody || hit.rigidbody.isKinematic)
return;
if (!springJoint)
{
go = new GameObject("Rigidbody dragger");
var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
springJoint = go.AddComponent ("SpringJoint");
body.isKinematic = true;
}
springJoint.transform.position = hit.point;
if (attachToCenterOfMass)
{
var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
anchor = springJoint.transform.InverseTransformPoint(anchor);
springJoint.anchor = anchor;
}
else
{
springJoint.anchor = Vector3.zero;
}
springJoint.spring = spring;
springJoint.damper = damper;
springJoint.maxDistance = distance;
springJoint.connectedBody = hit.rigidbody;
StartCoroutine ("DragObject", hit.distance);
}
function DragObject (distance : float)
{
springJoint.axis = Vector3.zero ;
var oldDrag = springJoint.connectedBody.drag;
//var oldAngularDrag = springJoint.connectedBody.angularDrag;
springJoint.connectedBody.drag = drag;
//springJoint.connectedBody.angularDrag = angularDrag;
var mainCamera = FindCamera();
while (Input.GetMouseButton (0))
{
var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
springJoint.transform.position = ray.GetPoint(distance); ;
yield;
}
if (springJoint.connectedBody)
{
springJoint.connectedBody.drag =oldDrag ;
//springJoint.connectedBody.angularDrag = oldAngularDrag;
springJoint.connectedBody = null;
}
hit.rigidbody.velocity = new Vector3(0, hit.rigidbody.velocity.y, 0);
hit.rigidbody.angularVelocity = Vector3.zero ;
}
function FindCamera ()
{
if (camera)
return camera;
else
return Camera.main;
}
What this script does is when you click on the platform (which is a touchable object), it created a gameObject in the scene (with no parent) and when you drag, you move around that new gameObject. This gameObject has a SpringJoint as a component, and the SpringJoint is linked to the platform. So when you're dragging the created gameObject, the SpringJoint between it and the platform works its magic to move the platform.
Moreover, we can add constraints to the rigidbody of the platform, so if we freeze the y and z axis, the platform only moves on the x axis.
Woohoo ! We got our draggable platform ! :)
PROBLEM IS : the 3 littles cubes above the platform react to the platform moving, thus moving with it. But they are moving along the platform frozen axis, y and z. Thus, if a user click on a platform and push the mouse upward, the platform is still (axis frozen) but the little cubes above will jump!
We tried to trick a little bit the script by changing the transform.position of the created gameObject to stays all the time the same as the y and the z of the platform. Doing so, the SpringJoint works only on the difference between the platform and the created gameObject : the x-axis. But the 3 littles cubes are still jumping on the y axis and sliding on the z one.
Can you please help us make a platform on which objects will only reacts to the x axis ? We would really appreciate it ! :)
Your answer