- Home /
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..
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.
ok Fornoreason1000 sir. it seems to be very difficult for me as i am in initial stage of unity.
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
@fafase sir,, thanks for helping me. I will look for projectile motion in 3d.
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;
}
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;
}
}
Your answer
Follow this Question
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