Grab and drag object in physical and realistic way

Is there any method that I can grab object from any point on object and drag them realistic way.
When I grab it, I want it to settle down by mass axis. Like this:
[70608-adsız.jpg|70608]

Then when it’s accelerating. It goes like this:
70609-1.jpg

When I tried to do this with springjoin2D, I cant make the same behaviour like above.
Then, I did this with hinge joint but I can’t avoid this object from overlapping with other object, is there any other way to do this? or Where did I make mistake here is my script.

here I tried to achive connected anchor to follow my mouse position. Object behavior like above but with one mistake, object can overlap collision area of other object.

I understand why this problem occur:
Because I am teleporting object to mouse position so it ignores collision effect…
But I cant figure out the solution

using UnityEngine;
using System.Collections;
public class MouseJoint : MonoBehaviour {
	Vector2 mousePos2D;
	SpringJoint2D sj;
	void Start() {
		sj = gameObject.GetComponent<SpringJoint> ();
	}
	// Update is called once per frame
	void Update () {
		Vector3 mouseWorldPos3D = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		mousePos2D = new Vector2(mouseWorldPos3D.x, mouseWorldPos3D.y);
	}
	void FixedUpdate ()
	{	
		sj.connectedAnchor = mousePos2D;
	}
}

`

I thing it is very fundemental to grab object like this, and drag it physically.
I am very beginner to unity
And sorry for my bad english

`

2 Answers

2

@omer1476
You can get this functionality by using new UI system.
Just make a UI Canvas and make new image attach your sprite to this image. After this attach the following script to your image and register the methods that are in script.

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

public class DragObject : MonoBehaviour {
	
	private Vector2 prevDragPos;
	
	public void OnBeginDrag(BaseEventData data) 
	{
		prevDragPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
	}

	public void OnDrag(BaseEventData data)
	{
		Vector3 curPos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
		
		
		float x = curPos.x - prevDragPos.x;
		float y = curPos.y - prevDragPos.y;
		
		Vector3 resPos = transform.position;
		resPos.x += x;
		resPos.y += y;
		transform.position = resPos;
		
		prevDragPos = curPos;
		
	}
}

Thank you, I used new canvas, ı created new image to this canvas, I attached my sprite to this image as a source image. I added this script to this image. But I don't understand how I register method. Should I use another script for my rigidbody or should I use this method in another place? [70637-121.jpg|70637]

Does this really answers the question? Why is the answer marked as correct? There's nothing here using physics (Unity's or custom), so I can't imagine how dragging something will make it move like it's hanging from a point. It does work for dragging something, but that's just part of the question.

you guys are awesome

I know it doesn't truly answer the question. Unfortunately only Unity themselves would be able to provide a response to that question, and you may have better luck getting that from the Unity Forums instead of Unity Answers, but even that is a toss up sometimes. If you do find the true answer please post it and mark it as the best answer over this one. As you said, the image does suggest that it simply just runs after the code has finished.