Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class BallControl : MonoBehaviour
- {
- private GameObject cueBall;
- public float power = 10.0f;
- public float maxDrag = 5.0f;
- private LineRenderer lr;
- private Rigidbody rb;
- Vector3 dragStartPos;
- Touch touch;
- private void Start()
- {
- cueBall = GameObject.Find("CueBall");
- rb = cueBall.GetComponent<Rigidbody>();
- lr = cueBall.GetComponent <LineRenderer>();
- }
- private void Update()
- {
- TouchActions();
- }
- void TouchActions()
- {
- if (Input.touchCount > 0)
- {
- touch = Input.GetTouch(0);
- if (touch.phase == TouchPhase.Began)
- {
- DragStart();
- }
- if (touch.phase == TouchPhase.Moved)
- {
- Dragging();
- }
- if (touch.phase == TouchPhase.Ended)
- {
- DragRelease();
- }
- }
- }
- void DragStart()
- {
- dragStartPos = Camera.main.ScreenToWorldPoint(touch.position);
- dragStartPos.y = 0;
- lr.positionCount = 1;
- lr.SetPosition(0,dragStartPos);
- }
- void Dragging()
- {
- Vector3 draggingPos = Camera.main.ScreenToWorldPoint(touch.position);
- draggingPos.y = 0;
- lr.positionCount = 2;
- lr.SetPosition(1,draggingPos);
- }
- void DragRelease()
- {
- lr.positionCount = 0;
- Vector3 dragReleasePos = Camera.main.ScreenToWorldPoint(touch.position);
- dragReleasePos.y = 0;
- Vector3 force = dragStartPos - dragReleasePos;
- Vector3 clampedForce = Vector3.ClampMagnitude(force, maxDrag) * power;
- Debug.Log(clampedForce);
- rb.AddForce(clampedForce,ForceMode.Impulse);
- }
- }
Add Comment
Please, Sign In to add comment