How to add force on curved swipe?

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;
}

}

Hey there!

So this is a slight change on some code I had already made, I’d wanted to originally implement a non-linear least squares method to find a curve equation, but at the time I was unable to (might try again at some point).

This is rather primitive in comparison to my initial plan, but should work for what you want to do.

When the mouse is down, every time its position has changed more than ‘tolerance’ (5 pixels by default) it saves the mouse position.

Once the mouse is released it runs through this list of points and works out the overall direction from the start and end points, and then based on that measures how many times the drag crossed the central position and its maximum offset each side.

It saves the direction to ‘direction’, the list of offsets from centre in ‘curveOffsets’ (you can then use that to apply some force sideways, or calculate a curve from the direction and curve peak), and it saves if there was more than just one point saved into ‘kicked’ so you know the mouse actually was dragged!

Hope that helps :slight_smile:

List<Vector2> pointTrack;
bool mouseDown;

bool kicked = false;
Vector2 direction;
Vector2[] curveOffsets;

void Start () {
	mouseDown = false;
}

void Update () {
	if (Input.GetMouseButtonDown(0)){
		pointTrack = new List<Vector2>();
		mouseDown = true;
		StartCoroutine(SavePoints());
	}

	if (Input.GetMouseButtonUp(0)){
		mouseDown = false;
	}

	if(kicked){
		kicked = false;

		Debug.Log("Kick overall direction: " + direction);

		foreach(Vector2 v in curveOffsets){
			Debug.Log("Curve " + v);
		}
	}

}

IEnumerator SavePoints(){
	Vector2 lastPos = (Vector2)Input.mousePosition;
	Vector2 pos;
	pointTrack.Add(lastPos);

	float tolerance = 5*5; //5 pixel tolerance

	while(mouseDown){
		pos = (Vector2)Input.mousePosition;

		if((pos-lastPos).sqrMagnitude >= tolerance){
			pointTrack.Add(pos);
			lastPos = pos;
		}

		yield return null;
	}

	if(pointTrack.Count < 2){
		return false;
	}
	kicked = true;

	direction = pointTrack[pointTrack.Count-1]-pointTrack[0];

	if(pointTrack.Count < 3){
		return true;
	}

	float tolerance2 = 10;
	int dirChange = -1;
	Vector2 perpDir = new Vector2(-direction.y, direction.x).normalized; //left side
	int lastSide = 0;
	int side = 0;
	float largestOffset = 0;

	List<Vector2> offsets = new List<Vector2>();

	for(int i = 1; i < pointTrack.Count-1; i++){
		Vector2 offset = (pointTrack_-pointTrack[0]) - (Vector2)Vector3.Project(pointTrack*-pointTrack[0], direction);*_

* side = (int)Mathf.Sign(Vector3.Dot(perpDir, offset));*

_ if(offset.sqrMagnitude < tolerance2*tolerance2) continue;_

* if(side != lastSide){*
* dirChange ++;*
* lastSide = side;*
* largestOffset = 0;*
* }*

* if(offset.sqrMagnitude > largestOffset){*
* largestOffset = offset.sqrMagnitude;*

* if(dirChange < offsets.Count){*
* offsets[dirChange] = offset;*
* }else{*
* offsets.Add(offset);*
* }*
* }*
* }*
* curveOffsets = offsets.ToArray();*
}