x


Instantiate a prefab every certain distance while swipping (iOS)

I'm touching the touch screen and a prefab is instantiated where I have placed my finger.

Now, keeping my finger on the screen, I begin to swipe.

A prefab's clone is instantiated every say 10 pixels.

The result would be something like a dotted line along the swipe path.

How do I do this?

more ▼

asked Apr 29 '11 at 06:04 AM

schwertfisch gravatar image

schwertfisch
370 53 57 68

Do you want it set up that you get x number of prefabs spread out per swipe, or you want one every x distance for the entire swipe?

Apr 29 '11 at 09:46 AM Joshua

The 2nd. A longer swipe will spawn more prefabs.

Apr 29 '11 at 11:35 AM schwertfisch

did you ever get a solution to this?

May 03 '12 at 07:52 PM ratboy
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

I'd do this by testing the touch position against the position of the last prefab and if it meets the threshhold - then create a new prefab.

Here is some untested C# code (my syntax checking isn't nearly as good as a compilers):

//last position where we drew the dot
Vector2 lastDotPos;
public float dotDropDist = 10; //min distance between dots

void Update () {
    foreach (Touch touch in Input.touches) {
       //if we start touching
       if (touch.phase == TouchPhase.Began) {
         Instantiate(prefabDot, new Vector3(touch.x, touch.y, 0, Quaternion.identity)
         lastDotPos = touch.position;
         break; //ignore the rest of the touches if any
       } else if (touch.phase == TouchPhase.Move) {
         //get the total pixels moved
         float distance = Mathf.Abs(touch.position.y - lastDotPos.y) + Mathf.Abs(touch.position.y - lastDotPos.y)
         //if we meet the threshold the create a new dot and reset our last dot position.
         if (distance > dotDropDist) {
          Instantiate(prefabDot, new Vector3(touch.x, touch.y, 0, Quaternion.identity)
          lastDotPos = touch.position;
         }
         break; //ignore the rest of the touches if any
       }
    }
}
more ▼

answered May 04 '12 at 11:00 PM

sumiguchi gravatar image

sumiguchi
20 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1667
x353
x237
x68

asked: Apr 29 '11 at 06:04 AM

Seen: 1379 times

Last Updated: May 04 '12 at 11:00 PM