Ok, i’ve gone through many tutorials, but still dumb enough to understand that how to detect a curved swipe?
I am trying to make a penalty shootout game and i want my football to travel a swinged path whenever a curved swipe is given as input.
somehow i’ve managed to figureout below code, but it only give a straight path to football, but i want a swing.
How to do it?
Any completed code overthere?
public class SwipeControl : MonoBehaviour {
public GameObject ball;
Vector3 firstpos, lastpos;
Vector3 force;
Vector3 startpos;
float starttime, endtime;
float dragdistance;
float forcefactor = 5.0f;
public bool returned = true;
void Start()
{
dragdistance = Screen.height * 10 / 100;
startpos = ball.transform.position;
}
void Update()
{
if (returned)
{
Player ();
}
}
void Player()
{
if (Input.GetMouseButtonDown (0))
{
Debug.Log(returned);
starttime = Time.time;
firstpos = Input.mousePosition;
}
if (Input.GetMouseButtonUp (0))
{
endtime = Time.time;
lastpos = Input.mousePosition;
Vector3 distance = lastpos - firstpos;
distance.z = distance.magnitude;
if (distance.x > dragdistance || distance.y > dragdistance)
{
Vector3 force = (distance / (endtime - starttime));
if (force.x > 400.0f)
{
force.x = 400.0f;
}
else if(force.x < -200.0f)
{
force.x = -200.0f;
}
if (force.y > 400.0f)
{
force.y = 400.0f;
}
else if(force.y < 200.0f)
{
force.y = 200.0f;
}
if (force.z > 400.0f)
{
force.z = 400.0f;
}
else if(force.z < 320.0f)
{
force.z = 320.0f;
}
Debug.Log (force);
ball.rigidbody.AddForce (force);
returned = false;
StartCoroutine (Returnball ());
}
}
}
IEnumerator Returnball()
{
yield return new WaitForSeconds (5.0f);
ball.rigidbody.velocity = Vector3.zero;
ball.rigidbody.angularVelocity = Vector3.zero;
ball.transform.position = startpos;
returned = true;
}
}