# Restricting body movement withing a specific length while dragging a gameobject in unity

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.

• 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. Aug 21 '15 at 8:01
• 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. Aug 21 '15 at 9:45
• – Anko
Oct 23 '15 at 21:40

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.

• 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. Aug 22 '15 at 7:27
• 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. Aug 22 '15 at 8:49
• 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)); Aug 24 '15 at 8:10
• Sorry mate I am not expert enough to debug this logic , until I see the setup. Aug 24 '15 at 8:21
• See the updated question. Aug 24 '15 at 11:11