I’m using a script from a fellow Unity member that adds some multitouch features in a similar way the Mouse input functions work.
The script works great, and I am able to drag my 2D rigidbody around the screen using the following code;
void OnTouchDrag (Touch touch)
{
currentScreenPoint = new Vector3 (touch.position.x, touch.position.y, screenPoint.z);
pickedUp = true;
}
and then in Update() is where I am moving the object to wherever the users finger is touching, updating frame by frame.
Update(){
if (pickedUp) {
verticalSpeed = rigidbody2D.velocity.y;
horizontalSpeed = rigidbody2D.velocity.x;
Debug.Log ("xspd: " + verticalSpeed + " / yspd: " + horizontalSpeed);
gameObject.rigidbody2D.gravityScale = 0;
screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
Vector3 currentPos = Camera.main.ScreenToWorldPoint (currentScreenPoint);
transform.position = currentPos;
}
If the user drags their finger fast to “fling” the game object, I need the game object to maintain is course/speed and let gravity take over. I am setting the gravityscale to 0 while pickedUp is true, however when it becomes false then gravityScale returns to default. Everything works perfectly, and I actually have tested by adding AddForce when the object is released and the force is added.
Basically, I just need to figure out how to determine the direction and amount of force to add based on the direction and how quickly the user is moving their finger.
You can see in the above code, I added "verticalSpeed’ and “horzontalSpeed” in hopes that Unity would be able to get the rigidbody velocity while being dragged then I could just add force based on this once the object is released, but the velocities always remain 0, im guessing due to the way I am moving the object.
Hoping someone can shed some light on the best way to go about this!