I am fairly new in UNITY and currently i am working on a developing a POOL game with flick input. (u flick the CUE ball with your finger as input ).
I have succeeded in taking swipe input and move the ball with velocity as per the speed of swipe. what i need to do now is limit the swipe area to certain radius distance of the cue ball. currently the input is taken when u swipe anywhere in the screen. I want to restrict this input area to the CUE ball alone.
My code for the swipe input is as below (this is not my script though i have found it in the UNITY forum)
`
using UnityEngine;
using System.Collections;
public class TouchMonitor : MonoBehaviour {
public Rigidbody ball;
private Vector3 touchStart;
private Vector3 touchEnd;
private GameObject lineRenderer;
void Update () {
if (Input.touchCount > 0)
{
Touch t = Input.touches[0];
if (t.phase == TouchPhase.Began)
{
touchStart = t.position;
}
if (t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled)
{
touchEnd = t.position;
DoInput();
}
}
if (Input.GetButtonDown("Fire1"))
{
touchStart = Input.mousePosition;
}
if (Input.GetButtonUp("Fire1"))
{
touchEnd = Input.mousePosition;
if(ball.IsSleeping())
DoInput();
}
}
Vector3 getCueBallPosition()
{
Vector3 ballpos=new Vector3();
ballpos=ball.transform.position;
return ballpos;
}
void DoInput()
{
Vector3 p1 = new Vector3();
Vector3 p2 = new Vector3();
Vector3 cuePos=getCueBallPosition();
touchStart.z = Camera.main.nearClipPlane+.1f; //push it a little passed the clip plane for camera, just to make sure its visible on screen
touchEnd.z = Camera.main.nearClipPlane+.1f;
p1 = Camera.main.ScreenToWorldPoint(touchStart);
p2 = Camera.main.ScreenToWorldPoint(touchEnd);
CreateLine(p1,p2);
Vector3 v = p2-p1;
Debug.Log("Ball Speed");
ball.AddForce(v*5, ForceMode.Impulse);
}
//creates an ugly purple line from pos1 to pos2 in worldspace
void CreateLine(Vector3 p1, Vector3 p2)
{
Destroy(lineRenderer);
lineRenderer = new GameObject();
lineRenderer.name = "LineRenderer";
LineRenderer lr = (LineRenderer)lineRenderer.AddComponent(typeof(LineRenderer));
lr.SetVertexCount(2);
lr.SetWidth(0.001f, 0.001f);
lr.SetPosition(0, p1);
lr.SetPosition(1, p2);
}
}`
i tried to get current position of cue ball and check if the initial touch (touch start) is within certain area from the centre of cue ball. but i could not get it working.
If anyone can shed some light in this matter i would be very grateful.
thanks in advance.