- Home /
Add force based on swipe speed, direction, curve
Hi, everyone,
I am trying to make a football to be kicked based on swipe. By how much speed you swipe that much force after Touchphase.Ended should be applied to the ball and in the swipe direction.
One more problem is that I want to make the ball go in a direction of swipe curve. What I mean is I want to split a swipe into two more directions. For example, take a swipe which split from right to left and then left to right.
Your answers are appreciated. Please let me know if you have a solution
hey @kumarc123 man did u implemeted the problem u asked cz i also want to do exactly same.. plz help me out ... thanks in advance
@kumarc123 hey man did implemented tis logic cz m also doing the same.. i some how flicked the ball all i want it to make curve and also flick ball to amount of force plz do rply
Answer by robertbu · Aug 23, 2013 at 03:33 PM
I don't understand what you mean by 'curve'. In addition, the bit of code you are using to calculate the force to apply to the ball doesn't make much sense to me. Here is a bit of code to play with. With a default scene setup, it shoots the ball away from the camera. To use:
Create a new scene
Crate a plane (to put the ball on) at (0, -0.5, 0).
Crate a sphere at the origin (0,0,0).
Add a rigidbody
Attach the following script
The click down of the mouse must start on the ball:
#pragma strict
var factor = 100.0;
private var startTime : float;
private var startPos : Vector3;
function OnMouseDown() {
startTime = Time.time;
startPos = Input.mousePosition;
startPos.z = transform.position.z - Camera.main.transform.position.z;
startPos = Camera.main.ScreenToWorldPoint(startPos);
}
function OnMouseUp() {
var endPos = Input.mousePosition;
endPos.z = transform.position.z - Camera.main.transform.position.z;
endPos = Camera.main.ScreenToWorldPoint(endPos);
var force = endPos - startPos;
force.z = force.magnitude;
force /= (Time.time - startTime);
rigidbody.AddForce(force * factor);
ReturnBall();
}
function ReturnBall() {
yield WaitForSeconds(4.0);
transform.position = Vector3.zero;
rigidbody.velocity = Vector3.zero;
}
What I mean by curve is suppose in a single mouse drag user started dragging / swiping from left to right and then from right to left. Both movements will be done from a single swipe.
Gesture detection can be very complicated. If this is the only gesture you are detecting, you can collect points as the finger is moved. At the end of the gesture, you can compare the set of points against the line segment and find the point the maximum distance from the line segment. This will give you a measure of the curve you can use for whatever force you add.
Hey @robertbu, i need to set the speed of the ball from my powerbar(slider) and direction from the swipe. what should i change?
@$$anonymous$$ahesh4Unity - Use your slider to set the 'power' variable in the following modified script:
#pragma strict
var power = 1000.0;
private var startPos : Vector3;
function On$$anonymous$$ouseDown() {
startPos = Input.mousePosition;
startPos.z = transform.position.z - Camera.main.transform.position.z;
startPos = Camera.main.ScreenToWorldPoint(startPos);
}
function On$$anonymous$$ouseUp() {
var endPos = Input.mousePosition;
endPos.z = transform.position.z - Camera.main.transform.position.z;
endPos = Camera.main.ScreenToWorldPoint(endPos);
var force = endPos - startPos;
force.z = force.magnitude;
force.Normalize();
rigidbody.AddForce(force * power);
ReturnBall();
}
function ReturnBall() {
yield WaitForSeconds(4.0);
transform.position = Vector3.zero;
rigidbody.velocity = Vector3.zero;
}
Answer by Imawizrd · Aug 23, 2013 at 07:24 AM
This is a very well thought out, intelligent question! Therefore I shall answer it for you!
Firstly, by swipe I'm going to assume you mean a swipe gesture on a touch screen mobile device. By assuming this, it means I can possibly provide you with the information you need faster! Rather than wasting time waiting for a reply.
When you first begin your swipe you should save the starting position of your finger. When you have finished your swipe gesture you should use the ending finger position and compare it to your starting position. By doing so, you can calculate the distance your finger has travelled in the time of the swipe.
Another variable you will want to calculate is the speed at which the user completed this gesture. Again, at the start of the gesture take store the current time (Time.time). When the gesture has ended compare the time of when it ended to the time of when you first started the gesture. By comparing these times you will have the total time it took to complete the gesture.
Now! Once we have gathered the information we need (distance travelled and time it took) we can calculate a variable from it that we can use to determine the force we want to add to our ball.
Something like this may work: ForceAmount * (DistanceTravelled / TimeTaken)
So it could look like this: (100 * (100 / 1 second)) = 10,000 force
or
(100 * (100 / 2 second)) = 5,000 force
Using this method to calculate your force, you would just need to adjust one variable ( in this scenario, ForceAmount) to increase or decrease the force you use.
Unity is a great engine that has a lot of resources on the internet! I suggest heading to the following links to learn more about what you can do in Unity.
http://www.youtube.com/watch?v=QMWhtKjUr10
Good luck with your swiping football game thing, and have fun learning Unity! :)
I definitely agree you answer. But I already implemented this trick. The actual problem is if I use camera.ScreenToWorldPoint with end position.x and y and camera.nearcliplane will it work perfectly?
This is my conversion of worldpoint from screenpoint
worldAngle = thisCamera.ScreenToWorldPoint(new Vector3(endPosition.x, endPosition.y + 800, (thisCamera.nearClipPlane - 100) * 1.8f));
ball.AddForce(new Vector3((worldAngle.x objectSpeed), (worldAngle.y objectSpeed), (worldAngle.z * objectSpeed)));
Is the above code correct?
If it is correct then please le me know about how to add force based on swipe curve.
Because in a single swipe I can go left and then right and viceversa. For this please share your ideas
Answer by Adishah11 · Sep 20, 2018 at 05:42 AM
Check this asset its has both 2d and 3d implementation link: https://assetstore.unity.com/packages/tools/physics/2d-and-3d-swipe-ball-control-96912?aid=1100l3bpb
Your answer
Follow this Question
Related Questions
Circular zoom control? 1 Answer
Help please want to reset game 0 Answers
How can I add time to a timer on an object collision? 1 Answer
Player model doesn't stay on ground 1 Answer
How to destroy a gameobject when I collide with it? 2 Answers