Search Unity

c# SwipeController (for mobile)

Discussion in 'Scripting' started by kelvin-w, May 3, 2017.

  1. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    hi there!
    I created a script that hopefully will help alot of people with their project.
    with this script you can create your own 2d paper toss like game.
    with a few simple steps every one can use this.

    the script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class SwipeControllerV1 : MonoBehaviour
    4. {
    5.     public GameObject PrefabObj; //The object that will be spawned
    6.     public float calcSpeed; //A var to calculate the speed
    7.     public bool isSpawned = false;
    8.  
    9.     private float angle; //Calculate angle of the shot
    10.     private float dist; //Distance of the swipe (to calculate speed)
    11.     private Vector3 fp;   //First touch position
    12.     private Vector3 lp;   //Last touch position
    13.     private float dragDistance;  //minimum distance for a swipe to be registered
    14.  
    15.     void Start()
    16.     {
    17.         dragDistance = Screen.height * 15 / 100; //dragDistance is 15% height of the screen
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (Input.touchCount == 1) // user is touching the screen with a single touch
    23.         {
    24.             Touch touch = Input.GetTouch(0); // get the touch
    25.             if (touch.phase == TouchPhase.Began) //check for the first touch
    26.             {
    27.                 fp = touch.position;
    28.                 lp = touch.position;
    29.             }
    30.             else if (touch.phase == TouchPhase.Moved) // update the last position based on where they moved
    31.             {
    32.                 lp = touch.position;
    33.             }
    34.             else if (touch.phase == TouchPhase.Ended) //check if the finger is removed from the screen
    35.             {
    36.                 lp = touch.position;  //last touch position. Ommitted if you use list
    37.  
    38.                 //Check if drag distance is greater than 20% of the screen height
    39.                 if (Mathf.Abs(lp.x - fp.x) > dragDistance || Mathf.Abs(lp.y - fp.y) > dragDistance)
    40.                 {//It's a drag
    41.                     dist = Vector3.Distance(fp, lp); //Calculate distance
    42.                     dist = dist * calcSpeed; //Calculate speed based on distance
    43.                     Vector3 pos = lp - fp; //Calculate difference between the 2 locations
    44.                     angle = Mathf.Atan2(pos.y, pos.x) * Mathf.Rad2Deg; //Use the difference to calculate the angle
    45.                     //Debug.Log("angle: " + angle);
    46.                     //Debug.Log("dist: " + dist);
    47.                     SpawnObject(); //Spawning the object
    48.                 }
    49.                 else
    50.                 {   //It's a tap as the drag distance is less than 20% of the screen height
    51.                     Debug.Log("Tap");
    52.                 }
    53.  
    54.                 if (isSpawned == true)
    55.                 {   //If there already is a ball in the game
    56.                     GameObject Obj = GameObject.FindGameObjectWithTag("Projectile"); //Find the GameObject
    57.                     Destroy(Obj); //Destroying the GameObject
    58.                     isSpawned = false; //Now another one can be spawned
    59.                 }
    60.             }
    61.         }
    62.     }
    63.     void SpawnObject()
    64.     {
    65.         if (isSpawned == false)
    66.         {
    67.             GameObject Obj = Instantiate(PrefabObj, this.transform.position, Quaternion.identity) as GameObject; //Instantiate a gameObject on the location of the gameobject where this script is connected to.
    68.             if (Obj.tag != null)
    69.             {   //Check if the Object Prefab doesn't has a tag
    70.                 Obj.tag = "Projectile"; //Giving the Object the Tag
    71.             }
    72.             Vector3 dir = Quaternion.AngleAxis(angle, Vector3.forward) * Vector3.right; //Calculate the direction of where it is going to shoot
    73.             Obj.GetComponent<Rigidbody>().AddForce(dir * dist); //Give force to the Object to shoot it
    74.         }
    75.     }
    76. }

    to use this you need to place this script onto your game object which will shoot (example: a canon)
    make a prefab out of the projectile and "drag&drop" it in the "prefab Obj" slot.
    give the object which holds this script the tag "SwipeController".
    set the calc speed to whatever you like and just play.

    now you can shoot infinite projectiles they wont go away.
    to fix this (and also to give them gravity) add this script to the projectile.

    the script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PrefabObj : MonoBehaviour {
    6.     public float gravPower = 0.0f;
    7.  
    8.     private void Start()
    9.     {
    10.         GameObject theObject = GameObject.FindGameObjectWithTag("SwipeController"); //find the object with the SwipeController script
    11.         SwipeController sc = theObject.GetComponent<SwipeController>();//Connect with the SwipeController script
    12.         sc.isSpawned = true; //Setting the Bool of the SwipeController script
    13.    
    14.     }
    15.     void Update () {
    16.         Physics.gravity = new Vector3(0, gravPower, 0); //Give gravity to the object
    17.     }
    18. }
    19.  

    now the projectile will disapear before a new one can be instantiated. (they disapear when you click or swipe on your screen)
    give the object which holds this script the tag "Projectile".

    dont forget the add the tags to the tag list!!
    if there are any mistakes, problems or bugs please let me know!



    enjoy your game!
    PS: this can only be tested on a smartphone!

    thanks alot for reading,
    kelvin-w
     
    Last edited: May 3, 2017
    JayGarxP likes this.
  2. UnSkiLd

    UnSkiLd

    Joined:
    Mar 4, 2015
    Posts:
    20
    Nice thanks for posting.
     
  3. kelvin-w

    kelvin-w

    Joined:
    Nov 25, 2016
    Posts:
    74
    no problem