Move a sphere along the swipe?

Posted by gameOne on Game Development See other posts from Game Development or by gameOne
Published on 2014-07-24T06:31:47Z Indexed on 2014/08/23 10:38 UTC
Read the original article Hit count: 332

Filed under:
|
|

I am trying to get a sphere curl based on the swipe. I know this has been asked many times, but still it's yearning to be answered. I have managed to add force on the direction of the swipe and it works near perfect. I also have all the swipe positions stored in a list. Now I would like to know how can the curl be achieved. I believe the the curve in the swipe can be calculated by the Vector dot product

enter image description here

If theta is 0, then there is no need to add the swipe. If it is not, then add the curl. Maybe this condition is redundant if I managed to find how to curl the sphere along the swipe position

The code that adds the force to sphere based on the swipe direction is as below:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SwipeControl : MonoBehaviour
{
//First establish some variables
private Vector3 fp; //First finger position
private Vector3 lp; //Last finger position
private Vector3 ip; //some intermediate finger position

private float dragDistance;  //Distance needed for a swipe to register
public float power;
private Vector3 footballPos;
private bool canShoot = true;
private float factor = 40f;

private List<Vector3> touchPositions = new List<Vector3>();


void Start(){

    dragDistance = Screen.height*20/100;
    Physics.gravity = new Vector3(0, -20, 0);
    footballPos = transform.position;
}

// Update is called once per frame
void Update()
{
    //Examine the touch inputs
    foreach (Touch touch in Input.touches)
    {
        /*if (touch.phase == TouchPhase.Began)
        {
            fp = touch.position;
            lp = touch.position;

        }*/

        if (touch.phase == TouchPhase.Moved)
        {
            touchPositions.Add(touch.position);
        }

        if (touch.phase == TouchPhase.Ended)
        {

            fp = touchPositions[0];
            lp = touchPositions[touchPositions.Count-1];
            ip = touchPositions[touchPositions.Count/2];





            //First check if it's actually a drag
            if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
            {   //It's a drag






                //Now check what direction the drag was
                //First check which axis
                if (Mathf.Abs(lp.x - fp.x) > Mathf.Abs(lp.y - fp.y))
                {   //If the horizontal movement is greater than the vertical movement...
                    if ((lp.x>fp.x) && canShoot)  //If the movement was to the right)
                    {   //Right move
                        float x = (lp.x - fp.x) / Screen.height * factor;
                        rigidbody.AddForce((new Vector3(x,10,16))*power);
                        Debug.Log("right "+(lp.x-fp.x));//MOVE RIGHT CODE HERE
                        canShoot = false;
                        //rigidbody.AddForce((new Vector3((lp.x-fp.x)/30,10,16))*power);
                        StartCoroutine(ReturnBall());

                    }
                    else
                    {   //Left move
                        float x = (lp.x - fp.x) / Screen.height * factor;
                        rigidbody.AddForce((new Vector3(x,10,16))*power);
                        Debug.Log("left "+(lp.x-fp.x));//MOVE LEFT CODE HERE
                        canShoot = false;
                        //rigidbody.AddForce(new Vector3((lp.x-fp.x)/30,10,16)*power);
                        StartCoroutine(ReturnBall());
                    }
                }
                else
                {   //the vertical movement is greater than the horizontal movement
                    if (lp.y>fp.y)  //If the movement was up
                    {   //Up move
                        float y = (lp.y-fp.y)/Screen.height*factor;
                        float x = (lp.x - fp.x) / Screen.height * factor;
                        rigidbody.AddForce((new Vector3(x,y,16))*power);
                        Debug.Log("up "+(lp.x-fp.x));//MOVE UP CODE HERE
                        canShoot = false;
                        //rigidbody.AddForce(new Vector3((lp.x-fp.x)/30,10,16)*power);
                        StartCoroutine(ReturnBall());
                    }
                    else
                    {   //Down move
                        Debug.Log("down "+lp+" "+fp);//MOVE DOWN CODE HERE
                    }
                }

            }
            else
            {   //It's a tap
                Debug.Log("none");//TAP CODE HERE
            }

        }
    }

}


IEnumerator ReturnBall() {

    yield return new WaitForSeconds(5.0f);
    rigidbody.velocity = Vector3.zero;
    rigidbody.angularVelocity = Vector3.zero;
    transform.position = footballPos;
    canShoot =true;
    isKicked  = false;
}

}

© Game Development or respective owner

Related posts about unity

Related posts about touch