How to drag and throw 2D objects?(SOLVED)

I have a script for dragging objects with my mouse while click1 is pressed but I don’t know how to throw them on release.I want the objects to be thrown at the same speed I move the mouse while I’m dragging it. I want to make a character who has special powers kind of like The Force(star wars) but in 2D.

This is what I use for dragging:

    void OnMouseDrag()
		Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
		point.z = gameObject.transform.position.z;
		gameObject.transform.position = point;
		Screen.showCursor = false;

	void OnMouseUp()
		Screen.showCursor = true;


Look below for answer!

I found a video that shows how I would like the dragging to be.

Youtube Video


using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {
	public SpringJoint2D spring;

	void Awake()

	spring = this.gameObject.GetComponent<SpringJoint2D>(); //"spring" is the SpringJoint2D component that I added to my object
	spring.connectedAnchor = gameObject.transform.position;//i want the anchor position to start at the object's position

	void OnMouseDown()
	spring.enabled = true;//I'm reactivating the SpringJoint2D component each time I'm clicking on my object becouse I'm disabling it after I'm releasing the mouse click so it will fly in the direction i was moving my mouse


	void OnMouseDrag()		
			if (spring.enabled = true) 

				Vector2 cursorPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);//getting cursor position
				spring.connectedAnchor = cursorPosition;//the anchor get's cursor's position


	void OnMouseUp()		
	spring.enabled = false;//disabling the spring component


Values for SpringJoint2D Component:
Distance: 0.005(minimum)
Damping Ratio: 0

I also changed the Ridibody2D Linear Drag value to 1 so the spring oscilation would stop.

I have’t played too much with the values but the ones above are ok for me.

How I got to this solution?:
I posted a video on youtube above showing Dragging Physics handled in Corona SDK, so i searched for the .lua file and saw they were using a Joint to do it.

if( Input.GetMouseButtonDown(0))

        Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
        RaycastHit hit;

        if( Physics.Raycast(ray, out hit) )
            Debug.Log( hit.point );
            firstClickPosition = hit.point;

// here your code “firstClickPosition is your click position”


void OnMouseDrag() {

  1. Vector3 MousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
  2. MousePos.z = 1f; //Optional
  3. transform.position = MousePos;
  4. }

void OnMouseUp() {

  1. Vector3 MousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
  2. Vector2 dir = MousePos - transform.position;
  3. dir.Normalize ();
  4. GetComponent.velocity = dir * 10f;
  5. }

I guess the most up-to-date answer is this one:

Hi everyone, I am trying to make use of OnMouseDrag to limit the speed at which a UI slider can be moved. The slider is used to scrub through an animation. I want to limit the speed at which the UI slider can be moved so that the animation is played smoothly.