- Home /
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);
}
}
Answer by asafsitner · Oct 04, 2011 at 08:59 AM
Look for 'constraints' section in the Rigidbody component. You can also save yourself the need to zero the Z axis value.
Huh? That only limits me to the movement. But how do I make the movement in the first place? (like the above example in my qn).
That limits either rotation or movement (or both) on either of the axes. To actually apply the force use Rigidbody.AddForce(direction * forceToUse);
as seen in the documentation:
http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddForce.html
ok.
but how do I get the direction? Let's say my origin is 0,0 and my thumb is at -1,-1. So what's the direction of my catapult in this case? (How to calculate it to be precise)
Answer by burrowgames · Oct 05, 2011 at 04:45 AM
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.