- Home /
Question by
schwertfisch · Apr 29, 2011 at 06:04 AM ·
instantiatedistancepathswipe
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?
Comment
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?
Answer by sumiguchi · May 04, 2012 at 11:00 PM
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
}
}
}