- Home /
3D Projectile Prediction Plane
I want to make a 3D pong style game. I thought of making a prediction plane to tell the player where the ball may go. This was going to be a vertical plane because the ball would not be affected by gravity because pong's ball was never affected by gravity. I have no idea how this would be done because I am new to game design. I would appreciate the help.
I think this is more math than game design. You will need to calculate the path the ball will take in order to find the point its going to end up. As with the old pong, I believe the bounce angle is decided by how far the ball lands from the center of the paddle. So in your case, because it will be a plane paddle ins$$anonymous$$d of a line paddle, every point on the plane will relate to a certain bounce angle.
When you know the bounce angle and the position of the paddle, you can figure out the position it is going to end up. $$anonymous$$aybe it will bounce of the wall so you will need to do some extra calculation for that. At the end, what you want, is a function where you can enter the position of the paddle and the location of the paddle hit, which will output a position on the other side of the playing field. I wouldn't worry about the wall bouncing just yet. I think that if you can get this working, flipping a direction when hitting a wall will be easy-peasy.
This would be the way I would try to accomplish this. So I hope it will help you.
Answer by highpockets · Apr 19, 2019 at 02:51 PM
You can raycast out and draw a line renderer to show your predicted path and you could show as multiple bounces, but you would likely want to cap the amount of predicted bounces to 4 for example or it could get quite messy. You can also have an effect to notify players if the prediction is such that the ball bounces out of bounds if the paddle isn’t in the right spot. When you know the direction that the ball is travelling, let’s say it always moves along its positive x axis for example:
Vector3 ballDir = ball.transform.right;
Just do raycasts against colliders and return the reflected direction vector and keep doing that for the amount of reflections / predicted bounces that you want to show and save each reflection point in an array that you pass to the line renderer:
public LineRenderer predictionLine;
Vector3 [] reflectionPoint;
void Start(){
predictionLine.positionCount = 5;
reflectionPoint = new Vector3[predictionLine.positionCount];
}
void Update(){
reflectionPoint[0] = ball.transform.position;
Vector3 ballDir = ball.transform.right;
Vector3 predictionDir = ballDir;
RaycastHit hit;
for(i = 1; i < reflectionPoint.Length; i++){
if (Physics.Raycast(reflectionPoint[i - 1], predictionDir, out hit, Mathf.Infinity)){
reflectionPoint[i] = hit.point;
predictionDir = Vector3.Reflect( predictionDir, hit.normal);
}
else
{
//if raycast doesn’t hit a Collider make the remaining line renderer point positions offscreen in-line with the last prediction direction
reflectionPoint[i] = reflectionPoint[i - 1] + ( predictionDir * 1000 ); //you can also do something to notify that the ball is predicted to go out of bounds here
}
}
predictionLine.SetPositions(reflectionPoint); //show prediction via line renderer
}
Hopefully that helps and you get the idea. Code not tested
Your answer
Follow this Question
Related Questions
Trajectory Prediction with Particle Systems 1 Answer
Can't think of a way to get slow moving projectiles to sync across a network. 1 Answer
2D projectile trajectory prediction 2 Answers
Tank AI, 2d rigidbody projectile trajectory prediction 0 Answers
how to draw trajectory of rocket for faux gravity in unity???? 0 Answers