So I’ve decided to start a new tutorial series on how to create a Pac-Man Clone in Unity 5.5. The tutorial like all others can be viewed on the Weekly Coder YouTube channel. You can click the link below to watch.
Be sure to stop by weekly for new videos in the series.
Below are the assets available for download.
So… you’ve got a useful method that does some pretty cool stuff but you don’t know where to put it. You’ve probably labeled this in your mind as a helper method which can be easily defined as a method that specifically aids in one or more repetitive tasks.
Great you say.. lets just create a new C# script and call it HelperMethods and throw it in there. This is not a bad idea and honestly I used to do this a lot myself but… there is a more elegant approach to this.
What if there was a way that you could add functionality to an already existing class? What if for instance you wrote a method which could easily be part of the Random class that is part of the UnityEngine?
This is actually exactly what I did. I wrote a method that returns not just a random range between min and max values but found that I needed to return a random number with specific numbers being excluded between the min and max values.
This is a perfect example of a method that should be an extension method to the Random class. So how do we do it? Its actually really simple. First know that we can place all of our extension methods within one simple class. Lets call this class ExtensionMethods.
While working in Unity I found it difficult to figure out how to get an object to rotate. If you’re reading this, you probably did too. Right now I’m just talking about 2D game object rotation. The concept works in 3D as well, just turn on a different axis, but I won’t go into that here.
If you don’t feel like reading the article and just want the Full Script… skip to the bottom.
Add this to the property section of your class:
public float degreesPerSec = 360f;
This will be the variable that tells the script how fast to rotate around the axis. It’s in degrees per second. Feel free to play with the number as see how it affects the rotation speed.