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…
-
How can i make this reference line feature?
-
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…
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;
}
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;
}
}