Advertisement
Guest User

Untitled

a guest
Feb 15th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.62 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SwipeDetect : MonoBehaviour
  6. {
  7.     private bool swiping = false;
  8.     private float length = 0;
  9.     private Vector3 startPos;
  10.     private Vector3 endPos;
  11.     private Vector3 final;
  12.     public float ballHeight = 1.893F;
  13.     private Vector3 ballStart;
  14.  
  15.     public GameObject catapultLeft;
  16.     public GameObject catapultRight;
  17.  
  18.     private SpringJoint leftJoint;
  19.     private SpringJoint rightJoint;
  20.  
  21.     public bool newBall = false;
  22.  
  23.     // Start is called before the first frame update
  24.     public void Start()
  25.     {
  26.         leftJoint = catapultLeft.GetComponent<SpringJoint>();
  27.         rightJoint = catapultRight.GetComponent<SpringJoint>();
  28.     }
  29.  
  30.     // Update is called once per frame
  31.     void Update()
  32.     {
  33.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
  34.         {
  35.             ballStart = transform.position;
  36.             ballStart.y = 0;
  37.             final = Vector3.zero;
  38.             length = 0;
  39.             swiping = false;
  40.             Vector2 touchPos = Input.GetTouch(0).position;
  41.             startPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
  42.         }
  43.         if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary))
  44.         {
  45.             swiping = true;
  46.             Vector2 touchPos = Input.GetTouch(0).position;
  47.             Vector3 tempPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
  48.             Vector3 updatePos = (tempPos - startPos);
  49.             updatePos.y = ballHeight + (updatePos.z / 2);
  50.             //Debug.Log(ballStart + updatePos);
  51.             transform.position = ballStart + updatePos;
  52.             //Debug.Log(updatePos);
  53.         }
  54.         if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Canceled))
  55.         {
  56.             swiping = false;
  57.         }
  58.         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && swiping)
  59.         {
  60.             Vector2 touchPos = Input.GetTouch(0).position;
  61.             endPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
  62.             final = endPos - startPos;
  63.             final *= -1; //reverse vector for fling effect
  64.             length = final.magnitude;
  65.            
  66.             foreach (Component c in GetComponents<SpringJoint>())
  67.             {
  68.                 Destroy(c);
  69.             }
  70.  
  71.             catapultLeft.GetComponent<LineRenderer>().enabled = false;
  72.             catapultRight.GetComponent<LineRenderer>().enabled = false;
  73.  
  74.             newBall = true;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement