I am currently developing a baseball game with Unity. I need to know the sophisticated drop point to implement baseball defense.
private void Movement() {
Vector3 hitDirection = Quaternion.Euler(0 f, 0 f, 0 f) * hitter.transform.forward;
ballRigidbody.useGravity = true;
ballRigidbody.AddForce(hitDirection * 3000);
ballRigidbody.AddForce(Vector3.up * 10, ForceMode.Impulse);
StartCoroutine(StopBall(ballRigidbody));
}
IEnumerator StopBall(Rigidbody ballRigidbody) {
yield
return new WaitForSeconds(1 f);
if (ballRigidbody.gameObject != null) {
ballRigidbody.velocity *= 0.5 f;
if (ballRigidbody.velocity.magnitude > 0)
StartCoroutine(StopBall(ballRigidbody));
}
}
The above code is an example of a code that makes the ball move.
private Vector3 PredictBallPosition(Vector3 initialPosition, Vector3 initialVelocity) {
Vector3 position = initialPosition;
Vector3 velocity = initialVelocity;
for (float t = 0; t < maxSimulationTime; t += timeStep) {
position += velocity * timeStep;
}
return position;
}
I simply tried to solve it through a for, but I couldn't find the exact drop point. In another way, I implemented the parabola myself and moved it, but I gave up because the trajectory I wanted didn't come out.
Is there a good way to know the drop point of the ball?