3
\$\begingroup\$

enter image description hereenter image description here

private void OnMouseDrag()
{
    Vector3 screen_sp=new Vector3(Input.mousePosition.x,Input.mousePosition.y,screen_space.z);
    Vector3 cur_pos=Camera.main.ScreenToWorldPoint(screen_sp)+offset;
        if(pendulum_object.gameObject != null && pendulum_object.gameObject.rigidbody!=null)
        {
                transform.position=cur_pos;
                    Debug.Log ("Pendulum is being dragged left"+transform.position);
                    Debug.Log("Mouse position is:"+Input.mousePosition);
                    Debug.Log("Current position is:"+cur_pos);
        }
}

The problem is:The dragging of game object must be restricted to the length of string. The pendulum is joint using hinge joint to the world.

\$\endgroup\$
3
  • \$\begingroup\$ Try to find normalized direction vector between the string start and the end after physical simulation, then try move object at the dir_vector*string_length and see what happens. I'm not sure if this is possible because I'm not familiar with Unity. This comment is all that I can suggest about your question. \$\endgroup\$
    – Ocelot
    Aug 21 '15 at 8:01
  • \$\begingroup\$ I don't know what that means?I just need that the Gameobject must be restricted to the length of the string,which is the hingeJoint anchor to which it is connected. \$\endgroup\$ Aug 21 '15 at 9:45
  • \$\begingroup\$ Here's a question asking how to do this in the general sense. \$\endgroup\$
    – Anko
    Oct 23 '15 at 21:40
1
\$\begingroup\$

You should proceed in the following way

1) Get the input mouse position convert it to world co-ordinates

Vector3 direction = Camera.main.ScreenToWorldPoint (Input.mousePosition);

2) Now calculate the directional Vector3 form anchor of the hinge joint to the input mouse position.

Vector3 diff = direction - Anchor.transform.position;

3) The last step will be to restrict the Position of the pendulum along this directional vector as per the string length (float) . By normalising we get the direction , then we clamp the magnitude to max String Length.

pendulum_object.transform.position = diff.normalized * Mathf.Clamp (diff.magnitude, 0, StringLength);

Note: If the pendulum is a child of a game object and if the scale and position differs. Then you need to use pendulum_object.transform.localPosition and also take care of the scaling factor.

\$\endgroup\$
6
  • \$\begingroup\$ Thank you for the respose,but that code didn't work.The pendulum now behave abnormally.I forgot to tell that the pendulum is swinging in y and z direction only.x axis position didn't change. \$\endgroup\$ Aug 22 '15 at 7:27
  • \$\begingroup\$ I have tested the code , I have used it once for on screen game pad to control the thumb from moving outside the surrounding circle. I am sure there must be some physics or hierarchy issue messing around. If you can share a project with the pendulum setup. I might be able to fix it. \$\endgroup\$
    – Hash Buoy
    Aug 22 '15 at 8:49
  • \$\begingroup\$ This is the improved code,the pendulum is now restricted to y axis but no luck in the z axis. transform.position = new Vector3 (cur_pos.x,Mathf.Clamp (cur_pos.y,transform.position.y, pendulum_body.hingeJoint.connectedAnchor.y),Mathf.Clamp (cur_pos.z,-pendulum_object.transform.position.y,pendulum_object.transform.position.y)); \$\endgroup\$ Aug 24 '15 at 8:10
  • \$\begingroup\$ Sorry mate I am not expert enough to debug this logic , until I see the setup. \$\endgroup\$
    – Hash Buoy
    Aug 24 '15 at 8:21
  • \$\begingroup\$ See the updated question. \$\endgroup\$ Aug 24 '15 at 11:11

You must log in to answer this question.

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