How to make my single-player Pong game's win/loss between the User and the Enemy random?
Hi! I have made a Pong game which is single-player for Android. I have just one issue : My game has a Player Paddle object which is what the Player controls. The Enemy follows the ball. The speed of the Enemy is controllable. There are different levels wherein, every level up you go, the speed of the Enemy decreases. Now if I make the speed of the Enemy too fast, it never loses. And, it never wins if it's speed is too low. How can I make the win/loss streak random, wherein the Player wins once in a while, and so does the Enemy.
An answer was posted in another place but I don't have the skills to code it out as I am a beginner to Unity and would love it if someone could help me with the coding etc. that is required to execute it. It is written down :
"make it so that after the enemy paddle has hit the ball it automatically returns to the (vertical) middle of the screen. only once the ball returns to the (horizontal) middle of the screen does the enemy begin following the ball again."
Hope someone can help me with this. Cheers!
Answer by Fredex8 · Mar 09, 2016 at 06:51 PM
I'd suggest generating a random number on each move and multiplying the enemy paddle speed by that. ie. multiplying it's speed by 0.70 is going to slow it down and give it a 30% handicap for that movement but on the next move the random number might be 0.99 which will result in it going almost full speed.
float randomNumber = Random.value;
paddleSpeed = paddleSpeed * randomNumber;
You just need to work out the range of the handicap you want and make it less and less as the game gets harder. You could also think about a reaction time variable which would create a delay before it moves towards the ball and again randomise that. Put the two together and you should get something that looks more human and less automatic.
That's the general idea -- add some randomness -- but this particular idea is not good. The paddle will just have an unnatural jerky motion (if you reroll each frame.) Or will obviously be letting the player win (if you reroll once after each hit.)
The reaction time idea is fine. But as long as the paddle is tracking the current ball position, it will look completely fake, no matter what else you do. For example, a human would never track a mid-wall bounce. The first step to making it seem even a little human is estimating where it will hit your side. That takes a little bit of trig, which the OP probably can't program.
I didn't say to generate a random number each frame. I said on each movement. Perhaps I should have been clearer on what I meant by that. Each time the player returns the ball trigger the enemy paddle to generate a random number etc...
Right -- last sentence of the first paragraph covers this. It will zip to hit the ball once. Then, when you hit it back the second time, it will veeeery, slooowly creep. When we just saw it go fast. Looks like it's purposely letting you win.
A variation would be to have to just go a little slower each time. It might seem like you're wearing it out or something.
Answer by BG_BOMB · Mar 28, 2016 at 03:53 PM
Hi! Thanks, @Fredex8 and @Owen Reynolds, for your answers. I have implemented the idea of the Random.value thing. I got it working and it looks quite human now. I set the speed of the Enemy at a Random Range (9 and 3 - can be controlled) which changes when the Ball touches the Enemy Paddle.
My previous problem is solved in which, either the User or Enemy lost. Thanks for that.
I have one issue though - The speed 3 is a little too low for the enemy which could be generated by the computer randomly. Now, it remains too slow for the Enemy to hit the ball again after he already lost the previous point. Therefore, I had an idea of putting an invisible Trigger at the end of the screen which resets the speed of the Enemy to possibly a greater number through which the Enemy can at least return the next ball. I tried several times but I couldn't figure out how to use the function 'OnTriggerEnter' properly. It would be great if someone could help me with the code or give me a reference to a video which describes this solely. Sorry for being so helpless. Thanks.
Your answer
Follow this Question
Related Questions
Game got slow down On iphone 0 Answers
How can i make an animation like csgo case opening? 2 Answers
Help in conversion of variable types c# 1 Answer
Is C# or javascript easier to understand? 2 Answers
convert java to c# 1 Answer