Applying force to an object without rotating it.

I’m trying to replicate smth like Angry Bird’s catapult system whereby (in my case), the player presses on the object then drag his fingers backwards, without the object being dragged, and when he releases his finger, the object will fly/go towards the direction opposite of where he lift off his finger.

Here is my code:

var bSelected : boolean = false;
var bIsMoving : boolean = false;

private var forceToUse : Vector3;
private var direction : int;

function Update () {
	//Prepare for launch!
	Prepare();
}

//If clicked
function OnMouseDown () {
	//When the thing is not moving and isn't already selected
	if (bIsMoving == false && bSelected == false) {
		bSelected = true;
	}
}

//If not clicked
function OnMouseUp () {
	//When the thing stops moving and is already selected beforehand
	if (bIsMoving == false && bSelected == true) {
		//Shoot code here
		//rigidbody.AddRelativeForce(forceToUse);
		
		//Reset back
		bSelected = false;
		//Debug.Log(bIsMoving);
		//Debug.Log(bSelected);
	}
}

function Prepare() {
	if (bSelected && !bIsMoving) {
		//Where this object is at
		var imHere = transform.position;
		//We don't want the z axis (depth)
		imHere.z = 0;
		//Debug.Log(imHere);
		//Find where is the mouse at
		var mouseIsAt = Input.mousePosition;
		mouseIsAt.z = 0;
		//Debug.Log(Input.mousePosition);
		
		//Force to use - Difference between the two
		forceToUse = mouseIsAt - imHere;
		
		//Direction
		direction = Vector3.Angle(mouseIsAt,imHere);
		Debug.Log(direction);
		
	}
}

Look for ‘constraints’ section in the Rigidbody component. You can also save yourself the need to zero the Z axis value.

You want to get a Normalize the vector in the direction you want to go. If your thumb is at <-1,-1> you want the object to go to <1,1>. If your thumb is at <-5,-2>, you want your object to head in the direction of <5,2>. However you need to normalize it.

So what you do is calculate the length of the vector. Length^2 = x^2 + y^2.

Then you divide the vector you calculated, say <5,2> by the length, which in that case will be 5.385, so you end up with a vector <0.92847, 0.3714>

Now what you have is a Unit vector in the direction you want to go. You can apply your own force to that by simply multiplying your force. Although in your case you might want your force based on the distance the user is from the projectile… You can normalize, and based on the distance clamp it to a maximum value, say you want your force to only be 10 max per direction. So you would check to see if your length vector is greater than a specified amount, if it is, then normalize it, and multiply that value by your maximum force.