Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SwipeDetect : MonoBehaviour
- {
- private bool swiping = false;
- private float length = 0;
- private Vector3 startPos;
- private Vector3 endPos;
- private Vector3 final;
- public float ballHeight = 1.893F;
- private Vector3 ballStart;
- public GameObject catapultLeft;
- public GameObject catapultRight;
- private SpringJoint leftJoint;
- private SpringJoint rightJoint;
- public bool newBall = false;
- // Start is called before the first frame update
- public void Start()
- {
- leftJoint = catapultLeft.GetComponent<SpringJoint>();
- rightJoint = catapultRight.GetComponent<SpringJoint>();
- }
- // Update is called once per frame
- void Update()
- {
- if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
- {
- ballStart = transform.position;
- ballStart.y = 0;
- final = Vector3.zero;
- length = 0;
- swiping = false;
- Vector2 touchPos = Input.GetTouch(0).position;
- startPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
- }
- if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary))
- {
- swiping = true;
- Vector2 touchPos = Input.GetTouch(0).position;
- Vector3 tempPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
- Vector3 updatePos = (tempPos - startPos);
- updatePos.y = ballHeight + (updatePos.z / 2);
- //Debug.Log(ballStart + updatePos);
- transform.position = ballStart + updatePos;
- //Debug.Log(updatePos);
- }
- if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Canceled))
- {
- swiping = false;
- }
- if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended && swiping)
- {
- Vector2 touchPos = Input.GetTouch(0).position;
- endPos = new Vector3(touchPos.x / 100, 0, touchPos.y / 100);
- final = endPos - startPos;
- final *= -1; //reverse vector for fling effect
- length = final.magnitude;
- foreach (Component c in GetComponents<SpringJoint>())
- {
- Destroy(c);
- }
- catapultLeft.GetComponent<LineRenderer>().enabled = false;
- catapultRight.GetComponent<LineRenderer>().enabled = false;
- newBall = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement