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
0
Question by Ekta-Mehta-D · Nov 25, 2013 at 07:30 AM · physicstouchballkick

Soccer Ball Kick Help

Hello everyone,

I am working on soccer game. I have set up the ground , goal net and soccer ball.

Now the reference game (similar to that i need to make kick ) is here :

https://play.google.com/store/apps/details?id=com.gamegou.PerfectKick.google

In this game , when user drag the ball it is showing reference direction/line with curve so that user can understand the direction in which the ball will go..

1) How can i make this reference line feature?

2) Force ball in that direction with fix distance ??

I have written script is here :

 var ball: Transform; // drag the ball here
 
 function KickForce(dest: Vector3, angle: float): Vector3 {
   var dir = dest - ball.position;  // get target direction
   var h = dir.y;  // get height difference
   dir.y = 0;  // retain only the horizontal direction
   var dist = dir.magnitude ;  // get horizontal distance
   var a = angle * Mathf.Deg2Rad;  // convert angle to radians
   dir.y = dist * Mathf.Tan(a);  // set dir to the elevation angle
   dist += h / Mathf.Tan(a);  // correct for small height differences
   // calculate the velocity magnitude
   var vel = Mathf.Sqrt(dist * Physics.gravity.magnitude / Mathf.Sin(2 * a));
   return vel * dir.normalized * ball.rigidbody.mass;
 }
 
 // control the kick angle when needed: the greater the angle, the slower the kick
 var kickAngle: float = 15;
 
 function Update(){
   if (Input.GetMouseButtonDown(0)){
     // Create a ray from the current mouse coordinates
     var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit: RaycastHit;
     // if something tagged "Ground" is hit...
     if (Physics.Raycast(ray, hit) && hit.transform.tag == "Ground"){
       var kForce = KickForce(hit.point, kickAngle); // calculate the force and kick!
       ball.rigidbody.AddForce(kForce, ForceMode.Impulse);
     }
   }
 }

But with this script it is going in right direction at mouse click. I want to kick ball with touch/drag.

Can someone please help me.

Thanks in advance for your support and help..

Comment
Add comment · Show 5
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 Ekta-Mehta-D · Nov 25, 2013 at 09:47 AM 0
Share

Please someone help me..

avatar image Fornoreason1000 · Nov 25, 2013 at 10:43 AM 0
Share

basically you use Secondary physic's to deter$$anonymous$$e that path of the ball based on its angle of ejection and its velocity, except in 3d http://www.physicsclassroom.com/class/vectors/u3l2c2.cfm

this is a good site to start. now in soccer balls can curve when you spin them. https://thescienceclassroom.wikispaces.com/Physics+of+Soccer

use these fundamental to create an array of vectors, and apply them to a line renderer.

avatar image Ekta-Mehta-D · Nov 25, 2013 at 11:14 AM 0
Share

ok Fornoreason1000 sir. it seems to be very difficult for me as i am in initial stage of unity.

avatar image fafase · Nov 25, 2013 at 11:38 AM 2
Share

This is not really Unity issue, more a projectile motion issue. You need to define the path of the ball at t = 1, 2 ,3 ,4 and so on based on the initial variable.

One problem, you can get a pretty accurate path but it won't be the path your ball will take. This is because an easy way to do is to avoid air resistance and friction and so on, but Unity will do that. Look into projectile motion in 3D, there are already some answers here http://answers.unity3d.com/questions/404238/projectile-motiontrajectory-motion-in-unity-3d.html

avatar image Ekta-Mehta-D · Nov 25, 2013 at 01:11 PM 0
Share

@fafase sir,, thanks for helping me. I will look for projectile motion in 3d.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by VIPINSIRWANI · Nov 25, 2013 at 01:21 PM

Once see this code may be it will help you to get directions

if(rcldr.Raycast(ray,out hit , 20.0f)) { ring.rigidbody.isKinematic = false; Debug.DrawRay(ray.origin,hit.point);

         if(Input.touchCount > 0)
         {
             if(Input.GetTouch(0).phase == TouchPhase.Moved)
             {
                 apply_force = true;
                 Vector2 deltaposition = Input.GetTouch(0).deltaPosition;
                 X_Force = deltaposition.x;
                 Y_Force = deltaposition.y;
             }
             if(Y_Force > 45)
             {
                 Y_Force = 45;
             }
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
avatar image
0

Answer by aksh2143 · Sep 04, 2015 at 07:03 AM

hi Ekta Mehta , i don't understand what you are asking for but, i,ve created below scrip and its working quite well. have a look

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class SwipeControl : MonoBehaviour {

public GameObject ball; Vector3 firstpos, lastpos; Vector3 force; Vector3 startpos; Vector3 swingforce; public Vector3 WindSpeed;

     float starttime, endtime;
     float dragdistance;
     float forcefactor = 5.0f;
     
     public bool returned = true;
     public bool drag = false;
     public bool inAir = false;

 void Start()
 {
     dragdistance = Screen.height * 10 / 100;
     startpos = ball.transform.position;
 }
 
 
 void Update()
 {
     if (returned) 
     {
         Player ();
     }
 }
 
 
 void Player()
 {
     if (Input.GetMouseButtonDown (0)) 
     {
         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)/0.5f));

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

             ball.rigidbody.AddForce (force);
             inAir = true;
             returned = false;
             StartCoroutine (Returnball ());
         }


     }


 }

 void FixedUpdate()
 {
     if(inAir)
     {
     
     ball.rigidbody.AddForce(WindSpeed);
     }
 }

 void SetWind()
 {
     WindSpeed = new Vector3 (Random.Range (-5, 5), 0, 0);
 }



 IEnumerator Returnball()
 {
     yield return new WaitForSeconds (3.0f);
     ball.rigidbody.velocity = Vector3.zero;
     ball.rigidbody.angularVelocity = Vector3.zero;
     ball.transform.position = startpos;
     SetWind();
     Debug.Log (WindSpeed);
     inAir = false;
     returned = true;
 }

}

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

how do i make pinball ? 1 Answer

Best way to create a ball controlling system for a football game 0 Answers

How To Make Ball Bounce On Touch 1 Answer

A node in a childnode? 1 Answer

Trouble Understanding Vector3.Angle() Result 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