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);
}
}