Sign in to follow this  

Unity 5 - Trouble with Raycasting for a Pong Clone Opponent AI

This topic is 480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there!

I'm running into some trouble programming the opponent in this Pong clone where I want it to predict where the ball would be when it's coming towards it at the paddle zone. What I'm using to predict the ball's position relative to the Z-axis (which is where the the paddles move freely) is raycasting, tags, layers, and colliders; those are checked if the difficulty setting is on medium or hard mode when the ball triggers the opponent's ball detector.

 

The code and some debugging output is provided here:
http://answers.unity3d.com/questions/1229543/gpd-pong-trouble-with-raycasting-for-opponents-loo.html

 

What I'm basically doing in those difficulties is having the opponent predict the position in only two cases: when the ball has a straight path towards the opponent, and when the ball is one collision from the walls away from the opponent's strike zone (as some sort of look-ahead from a bounce).

 

Please take a look and help me out. Any resources you think I should look at can be helpful too, as I am definitely a beginner developer.

Share this post


Link to post
Share on other sites

I don't think I really have a specific question, but I can give you details on where I'm stuck and what I tried.

Let's say that the ball is traversing a straight path from the player to the opponent and does not hit a wall at all. The opponent is supposed to calculate the ball's Z position when its X position matches that of the opponent's paddle; simply put, in the region where the opponent's paddle moves up or down. Somehow, that case is not executed as the debug output I'm getting says the Z position is 0.0 and do not react. (What I'm using is a Boolean flag to have the opponent move its paddle into position, and a float denoting the position where to move the paddle to.)

The other case is where the ball has to bounce once before it can reach the opponent's paddle zone. That case is not executed either.

 

 

What I tried using is Physics.Raycast; in the first case, to simply see whether or not the collider I hit is the opponent's goal zone, and if yes, find the position the paddle moves to based on the ball's position and the paddle's position.  Basically, I am calculating some sort of scalar multiple to multiply the ball's speed vector by to get the ball's predicted Z position at the paddle zone. It's similar to the second case, except I use two Raycast because the first one has to hit either the north or south wall before I check whether the next raycast hits the goal zone (which is simply a Box Collider with a trigger).

There are two formulas I use for my scalar:
 

1 - scalar = (Mathf.Abs(transform.position.x) - Mathf.Abs(pongBall.getPosition().x)) / Mathf.Abs(pongBall.getSpeedVector().x);
2 - scalar = (Mathf.Abs(transform.position.x) - Mathf.Abs(collisionObject.normal.x)) / Mathf.Abs(Vector3.Reflect(pongBall.getSpeedVector(), collisionObject.normal).x);

I am using the X components of three Vector3 objects to get the scalar needed to multiply the speed vector of my Pong ball by to get the change in Z position.

I don't really have on-screen video footage to show you what's going on. Just the code, game data, and some photos to show what's happening.

Am I really making sense here? I don't know if I'm having a very hard time explaining this.

Share this post


Link to post
Share on other sites

Actually, I decided to tackle this problem something different, and probably more easier. What I did instead was simply using a scalar of the ball's speed vector, and then have a decision structure figure out which situation would fit based on the maximum distance the ball would travel in the Z axis at the time the ball was detected. This sounds eccentric, but it somewhat works.

 

Take a look at my Unity Answers post, and you'll see what I mean.

Edited by GPDUWGD100

Share this post


Link to post
Share on other sites

This topic is 480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this