0
\$\begingroup\$

I have a sequence of capsules linked with fixed join and each capsule have a Rigidbody and a cube collider (the picture bellow shows how it's look like) enter image description here

and this is the drag script I applied on it

public class MouseDrag : MonoBehaviour
{
Vector3 screenPoint;
Vector3 offset;

bool dragging = false;

Rigidbody rigid;

void Awake()
{
    rigid = GetComponent<Rigidbody>();
}

void OnMouseDown()
{
    screenPoint = Camera.main.WorldToScreenPoint(transform.position);
    offset = transform.position - Camera.main.ScreenToWorldPoint(
        new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));

    dragging = true;
}

void OnMouseUp()
{
    dragging = false;
}

void FixedUpdate()
{
    if (dragging)
    {
        Vector3 point = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
        Vector3 dest = Camera.main.ScreenToWorldPoint(point) + offset;

        rigid.AddForce((dest - rigid.position) * 200f);
        rigid.velocity *= 0.8f;
    }
}}

it works but at some point of the drag all the capsules explode and all the other components do the same thing

after some research I found some people says that the Rigidbody did the problem but I couldn't understand why or how it did the problem ?

Here is a link to a small video of the problem : https://drive.google.com/file/d/1Ia3X356L_VaIGIrDAdLyZHG9K5k71S1j/view?usp=sharing

\$\endgroup\$
7
  • \$\begingroup\$ I never did that but try to experiment with ForceMode: docs.unity3d.com/ScriptReference/ForceMode.html. Maybe this will help. \$\endgroup\$
    – kolenda
    Apr 17, 2019 at 8:52
  • \$\begingroup\$ I should change only the ´FixedUpdate´ function or the whole class ?? \$\endgroup\$ Apr 17, 2019 at 9:02
  • \$\begingroup\$ Just add one of the ForceMode values as second parameter to AddForce function and see if it helps. You can read AddForce documentation for examples. \$\endgroup\$
    – kolenda
    Apr 17, 2019 at 10:01
  • \$\begingroup\$ I tried it but it doesn't help .... I think the problem is in the Fixed-Joint and the distance between the object , when an object is far from the one that is linked with the problem is generated, so I think if I can set a maximum distance between object to keep them in the area of the joint the problem can be resolved !! ... do you have any Idea how set a maximum distance between objects linked with fixed-Joint? \$\endgroup\$ Apr 17, 2019 at 10:10
  • \$\begingroup\$ To do what you say you can use SpringJoint with max distance: docs.unity3d.com/ScriptReference/SpringJoint-maxDistance.html. If it doesn't help then could you make some video of your problems? It's hard to say what behavior you want to get and how it works currently. \$\endgroup\$
    – kolenda
    Apr 17, 2019 at 11:59

1 Answer 1

0
\$\begingroup\$

As can be seen in the video, the phenomenon appears when the dragged object gets far behind the dragging cursor.

This might be because the current code increases the force the further the cursor gets ahead of the object. So at some point it receives a really large amount of force, the joints break, and you have an object flying around like a bullet, crashing into other rigidbodies and causing the chain-reaction you see here.

To avoid this from happening, you might want to limit the maximum amount of dragging-force.

    const float MAX_DRAG_FORCE = 10.0f // adjust to taste - how high it should be depends on the scale of your simulation

    Vector3 point = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
    Vector3 dest = Camera.main.ScreenToWorldPoint(point) + offset;

    // calculate the force-vector
    Vector3 forceVector = dest - rigid.position;
    // reconstruct it from its direction and clamped magnitude:
    forceVector = forceVector.normalized * Mathf.Clamp(forceVector.magnitude, 0.0f, MAX_DRAG_FORCE);

    rigid.AddForce(forceVector * 200f);

I also omitted the rigid.velocity *= 0.8f; The documentation discourages you from changing the velocity because it "can result in unrealistic behaviour". Maybe this is part of what you are seeing here. If you don't want objects to slide around and slow down instead, increase the "Drag" value of the rigidbody.

\$\endgroup\$
4
  • \$\begingroup\$ I tried this code but still the same problem \$\endgroup\$ Apr 18, 2019 at 14:34
  • \$\begingroup\$ @AmineChoukri Did you experiment with reducing the MAX_DRAG_FORCE constant? \$\endgroup\$
    – Philipp
    Apr 18, 2019 at 15:34
  • \$\begingroup\$ Yes is i reduce it it's work but it become hard to drag it \$\endgroup\$ Apr 18, 2019 at 16:52
  • \$\begingroup\$ I want the intestine stay close to the cursor \$\endgroup\$ Apr 18, 2019 at 16:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .