- Home /
Trajectory prediction basketball
I need some help to get it right. I did some research and managed to get something done, but it isn't work how it should. I use a second ball to give the basketball force , angle ... aldonaletto script. I have some line renderer working but not how it is supposed to be. So if anyoane can help me that will be great. I have another plane who let me move the invisible ball up and down not only left and right. The prediction won't work in some positions(I get this error:rigidbody.velocity assign attempt for 'ball(Clone)' is not valid. Input velocity is { NaN, NaN, NaN }. UnityEngine.Rigidbody:set_velocity(Vector3) Force:Update() (at Assets/Scripts/Force.cs:98) ) and my ball don't follow the path everytime. If i want to hit the floor and then bounce to the hoop , first it make some sort of an arc then go to the ground and nothing else.
void Update()
{
if(Input.GetKey(KeyCode.Escape))
{
Application.LoadLevel(1);
}
var referenceForward = Vector3.up;
var referenceRight= Vector3.Cross(Vector3.up, referenceForward);
var newDirection = ball2.transform.position - playerFire.transform.position;
var angle = Vector3.Angle(newDirection, referenceForward);
unghi = angle;
// Distances = Vector3.Distance(Input.mousePosition, new Vector3(ball2.transform.position.x, ball2.transform.position.y, 0));
if(Input.GetButton ("Fire1"))
{
//path simulation
// simulatePath();
mousePoz = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
if(mousePoz != Vector2.zero)
{
ray = Camera.mainCamera.ScreenPointToRay(mousePoz);
if(Physics.Raycast(ray, out hit, Mathf.Infinity))
{
ball2.transform.position = new Vector3(hit.point.x, hit.point.y, 0);
}
}
}
if(Input.GetButtonUp("Fire1"))
{
rigidbody.useGravity = true;
rigidbody.velocity = BallisticVel(myTarget, shootAngle);
}
}
Vector3 BallisticVel(Transform target, float angle)
{
Vector3 direction = transform.position - target.position; // get target direction
float height = direction.y; // get height difference
direction.y = 0; //retain only the horizontal direction;
float distance = direction.magnitude; // get horizontal distance;
float a = unghi * Mathf.Deg2Rad; //convert angle to radians;
direction.y = distance * Mathf.Tan(a); //set direction to the elevation angle;
distance += height / Mathf.Tan(a); //correct for small height difference
// calculate the velocity magnitude
float velocity = Mathf.Sqrt(distance * Physics.gravity.magnitude / Mathf.Sin(2 * a));
return velocity * direction.normalized;
}
Here it is my update function and Ballistic. Now the simulation path function :
// void simulatePath()
// {
// Vector3[] segments = new Vector3[segmentCount];
// segments[0] = playerFire.transform.position;
// Vector3 segVelocity;
//
// Vector3 direction = transform.position - ball2.transform.position; // get target direction
// float height = direction.y; // get height difference
// direction.y = 0; //retain only the horizontal direction;
// float distance = direction.magnitude; // get horizontal distance;
// float a = unghi * Mathf.Deg2Rad; //convert angle to radians;
// direction.y = distance * Mathf.Tan(a); //set direction to the elevation angle;
// distance += height / Mathf.Tan(a); //correct for small height difference
// // calculate the velocity magnitude
// float velocity = Mathf.Sqrt(distance * Physics.gravity.magnitude / Mathf.Sin(2 * a));
// segVelocity = velocity * direction.normalized;
//
// for(int i = 1;i < segmentCount; i++)
// {
//
// if(segments[i] != ball2.transform.position)
// {
// float segTime = (segVelocity.sqrMagnitude != 0) ? segmentScale / segVelocity.magnitude : 0;
// segVelocity = segVelocity + Physics.gravity * segTime;
// RaycastHit hit;
//
// if(Physics.Raycast(segments[i-1], segVelocity, out hit, segmentScale))
// {
// segments[i] = segments[i-1] + segVelocity.normalized * hit.distance;
// segVelocity = segVelocity - Physics.gravity * (segmentScale - hit.distance) / segVelocity.magnitude;
// segVelocity = Vector3.Reflect(segVelocity, hit.normal);
// }
// else
// {
// segments[i] = segments[i-1] + segVelocity * segTime;
// }
// }
//
// }
// Color startColor = playerFire.nextColor;
// Color endColor = startColor;
// startColor.a = 1;
// endColor.a = 0;
// sightline.SetColors(startColor, endColor);
//
// sightline.SetVertexCount(segmentCount);
// for(int i=0; i < segmentCount; i++)
// sightline.SetPosition(i, segments[i]);
// }
i use only x and y but the ball after it been launched can bounce anyware
Thanks in advance and sorry for my bad english.
Answer by Andsomesan · Jul 19, 2013 at 02:32 PM
after some minor modification to segment scale and how many they are i made my ball follow the path. So that let me with those problems: how can i fix that error and how can i make my ball bounce from the ground to the hoof. I think that i will need much more force, but that sort of arc still be in my way to get it done.
Your answer
Follow this Question
Related Questions
How to create a 2D parabolic trajectory prediction line with javascript? 1 Answer
Trajectory prediction 0 Answers
2D Trajectory/Path Predictor 0 Answers
Trying to make a trajectory prediction line! 1 Answer
Calculate Velocity of Impulse 0 Answers