Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by kumarc123 · Aug 23, 2013 at 06:57 AM ·

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hgaur725 · Mar 13, 2014 at 12:38 PM 0
Share

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

avatar image hgaur725 · Mar 13, 2014 at 12:59 PM 0
Share

@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

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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;
 }
Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kumarc123 · Aug 26, 2013 at 06:29 AM 0
Share

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.

avatar image robertbu · Aug 26, 2013 at 02:34 PM 0
Share

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.

avatar image Mahesh4Unity · Feb 21, 2014 at 06:56 AM 0
Share

Hey @robertbu, i need to set the speed of the ball from my powerbar(slider) and direction from the swipe. what should i change?

avatar image robertbu · Feb 21, 2014 at 07:24 AM 0
Share

@$$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;
 }
avatar image Mahesh4Unity · Feb 21, 2014 at 07:47 AM 0
Share

@robertbu thanks, that's i want. :)

Show more comments
avatar image
2

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://unity3d.com/learn

http://www.youtube.com/watch?v=QMWhtKjUr10

Good luck with your swiping football game thing, and have fun learning Unity! :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kumarc123 · Aug 23, 2013 at 07:47 AM 0
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges