How do I figure out the end location of a moving object.
Hi, I am trying to figure out the end point of a trajectory object once force has been added. Ideally, when struck with a bat. I have tried using real world physics questions and my code below works great on high vertical arcs but lower and faster moving velocity gives me problems.In my basic test, I am looking for a plane to simple go where the ball will land. If anyone has idea what I am doing wrong or a better method, please let me know. Also I took these equations from the following video. https://www.youtube.com/watch?v=CixFQDpgjRU
private float ballVel;
private float timeToPeak;
private float maxHeight;
private float timeToHitGround;
private float hangTime;
private float hitDistance;
Vector3 startLocal;
Vector3 hitLocation;
private float local1;
private float local2;
//start calculations of angle
private bool findSecondPoint = false;
private bool testPoint = true;
Vector3 targetPos;
void Start () {
}
// Update is called once per frame
void Update () {
if (testPoint) {
GetComponent<Rigidbody> ().AddForce ((target.position - transform.position) * 150);
testPoint = false;
}
}
void OnTriggerExit(Collider col){
if (col.tag == "Host") {
startLocal = transform.position;
ballVel = GetComponent<Rigidbody> ().velocity.x ;
timeToPeak = ballVel / (9.81f);
maxHeight = (ballVel * timeToPeak - 4.9f) * timeToPeak * timeToPeak;
timeToHitGround = Mathf.Sqrt (maxHeight / 4.9f);
hangTime = timeToPeak + timeToHitGround;
hitDistance = ballVel * hangTime;
hitLocation = new Vector3 (startLocal.x + hitDistance ,.1f, startLocal.z );
planeTest.transform.position = hitLocation;
Debug.Log (ballVel);
Debug.Log (timeToPeak);
Debug.Log (maxHeight);
Debug.Log (hitDistance);
}
}
}
Check out this thread. There are a few approaches and formulas.
http://answers.unity3d.com/questions/248788/calculating-ball-trajectory-in-full-3d-world.html
You could also consider foregoing the physics engine itself and doing your own trajectories. The $$anonymous$$inematic Equations are well known and are obviously extensible to 2 / 3-D games, letting you know far in advance of a hit where your ball was going to land.
Your answer
Follow this Question
Related Questions
Vector3 Help - Calculating velocity toward a point 1 Answer
How to stop a sphere that is using GetAxis from moving? (Roll-A-Ball Tutorial) 0 Answers
Physics in car game: How to add drag on car from calculated resistances in Vector3 0 Answers
How to move an Instantiated Object towards a moving Object? 0 Answers
Null Reference Exception..Don't know the answer... 2 Answers