First, let’s just have a couple seconds to get our heads around that needless spoonerism of a title… okay? good.

So then the dev-blog, well if anyone has read the Goals post, they’ll know roughly where I am, and if you’re read part of my portfolio you might be a little confused so let’s set everything out here and now. 

For those who haven’t read the “Goals” post, I recently made a promise to re-create several classical games with a total of 8 games to choose from, with the first one already chosen – Pong… again. Some might question, “If you’ve made Pong once, then why on earth are you making it again?“.

In all honesty there are several reasons, first of all it’s a project I’m familiar with and therefore a good point to be getting back on with, secondly the first Pong game I made was in Javascript and part of the reason I left that project was because of how hard and stunted it would be to continue the game in ‘Unityscript’, the C# is far more versatile and transferable. Thirdly the original game was highly inefficient, right now I’m re-creating the AI (read the portfolio piece to understand what a pain this can be), and already the rackets, ball, gamemanager and so forth are far more efficient and waste far less CPU time with unnecessary calls and checks.

Capture(11)
The new and improved Pong!

But the first game still worked though, why go back?“, well I know a lot of gamedevs get really worried about having learnt something new and then go back and re-vamp older parts of the project so they’re up to snuff, and this can become a hazardous spiral to fall into. This isn’t that, I’m very proud that the game is ‘better’, even if it is only known to me, but this project is more than anything warming up my C# skills for the new challenges of the other classic games.

To give an example of how the game is improved compared to the old model, I’ll share the biggest issue I was having and have recently solved. In the original Javascript Pong, every single frame the AI would search for an object called “Ball(Clone)“, this was to find the instantiated prefab of the ball, and this happened every single fixed frame! That’s a lot of unnecessary calls! My comment to justify this wasted computing was “the ball gets destroyed a lot so the Find has to be utilised frequently“, not that frequently past me.

Capture(12)
It wasn’t my best game admittedly… damn AI, I wish you’d never found the ball!

This time I have the game manager have a function called “SetScene” to create the ball (which is invoked after a goal is scored to give a brief game pause before a new round), and from there it would call a function in the AI script to set the scripted ball object to the object called once again “Ball(Clone)“, but it wasn’t working… at all. This was strange as I even started to invoke the AI’s function a half second later so that it would hopefully find the ball, because maybe I was just asking too quickly? This answer barely worked, and when I say barely I mean maybe 1 in 5 balls would instantiate quick enough for it to work.

So what’s to be done? You need the AI to near instantly find the ball so that it can return a serve, otherwise it’s not a fair game and you’re just watching a ball fall into the opponent’s goal, it isn’t a matter of challenge at all. As it happens I found the answer courtesy of an outside of the box thinker on the Unity answer forums. They suggested setting the game object variable to actually equal the instantiate method, there was a minor issue of casting an object into a game object but it worked. Now the AI can instantly find the ball and act as a challenging opponent.

A somewhat simple solution perhaps, but I’d never thought of setting a variable as being equal to an instantiation, and it’s just one of those things that you’re never really aware of until it’s pointed out to you in such an obvious manner that you’ll never forget it. So if you’re having a similar issue perhaps this post might help you find that special gameobject out there! Until next time keep designing, have an awesome day, and thanks for reading!

PongRound2.gif
It’s again, not one of my finer games.
Advertisements

One thought on “Live Pong and Lrosper…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s