3
\$\begingroup\$

I am developing a game in unity with flying arrows that the user tries to deflect by swiping through them (changing their direction). I can get the arrows to move with the swipes, but they move no matter where the swipes occur. I want them to only move (be affected) iff the swipe goes through the arrow. Here's what I have so far:

public class swipe : MonoBehaviour 
{

private Vector2 leftFingerPos = Vector2.zero;
private Vector2 leftFingerLastPos = Vector2.zero;
private Vector2 leftFingerMovedBy = Vector2.zero;

public float slideMagnitudeX = 0.0f;
public float slideMagnitudeY = 0.0f;

bool touched = false;


void Update()
{
    if (Input.touchCount == 1)
    {
        Touch touch = Input.GetTouch(0);



        if (touch.phase == TouchPhase.Began)
        {
            leftFingerPos = Vector2.zero;
            leftFingerLastPos = Vector2.zero;
            leftFingerMovedBy = Vector2.zero;

            slideMagnitudeX = 0;
            slideMagnitudeY = 0;

            // record start position
            leftFingerPos = touch.position;

        }

        else if (touch.phase == TouchPhase.Moved)
        {
            leftFingerMovedBy = touch.position - leftFingerPos; // or Touch.deltaPosition : Vector2
            // The position delta since last change.
            leftFingerLastPos = leftFingerPos;
            leftFingerPos = touch.position;

            // slide horz
            slideMagnitudeX = leftFingerMovedBy.x / Screen.width;

            // slide vert
            slideMagnitudeY = leftFingerMovedBy.y / Screen.height;

            if(touched)
            {
                //rigidbody2D.velocity = new Vector2(slideMagnitudeX, slideMagnitudeY)*20;
                rigidbody2D.AddForce(new Vector2(slideMagnitudeX, slideMagnitudeY)*5000);
                touched = false;
            }
        }

        else if (touch.phase == TouchPhase.Stationary)
        {
            leftFingerLastPos = leftFingerPos;
            leftFingerPos = touch.position;

            slideMagnitudeX = 0.0f;
            slideMagnitudeY = 0.0f;
        }

        else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
        {
            slideMagnitudeX = 0.0f;
            slideMagnitudeY = 0.0f;
        }
    }

}

void OnMouseEnter()
{
    touched = true;
}
}

The only way I have been able to find so far is using "onMouseEnter" which only works for very slow swipes.

How do I detect whether a swipe goes through an object?

\$\endgroup\$
  • \$\begingroup\$ Your title is not a Google search query. It is the title to a question. Please fix it. \$\endgroup\$ – Fund Monica's Lawsuit Jan 12 '15 at 4:01
0
\$\begingroup\$

You have both the current position and the last position on move. (touch.position, leftFingerPos)

You can do a ray cast using these to see if there is a collision with your arrow.

http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.