- Home /
How to move 2D object on touch in Unity 4.3
Hi, I am moving a 2D object on same direction and on same force as user swipe on screen for iOS in Unity4.3 as my following code show but it takes swipe on whole screen. Now I want to move object only when user swipe on object like user pick object with finger and throw him on desire direction. Here is following code but it take swipe whole screen, I want to take swipe just on desire object.
if(Input.touchCount>0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
Vector2 touchdeltaposition = Input.GetTouch(0).deltaPosition;
anglex = (float) (touchdeltaposition.x * Time.deltaTime *10.0);
angley = (float) (touchdeltaposition.y * Time.deltaTime *10.0);
elevationAngle = new Vector3(anglex,angley,0);
Vector3 elevation = Quaternion.Euler(elevationAngle)*transform.forward;
rigidbody2D.AddForce(elevation);
rigidbody2D.velocity= new Vector2(anglex,angley);
}
Thanks in advance, this will help me a lot.
Answer by siddharth3322 · Jan 17, 2014 at 07:57 AM
If your above code work for throwing object then one thing you are missing here.
You have to detect that correct object get touch down or not? For this you have to attach collider to your touch object and manage flag for this.
Based on this flag value you have to apply your swipe detection code.