Trending
Opinion: How will Project 2025 impact game developers?
The Heritage Foundation's manifesto for the possible next administration could do great harm to many, including large portions of the game development community.
Featured Blog | This community-written post highlights the best of what the game industry has to offer. Read more like it on the Game Developer Blogs or learn how to Submit Your Own Blog Post
A post in which I introduce the first few lines of code I ever wrote while developing stuff in Unity3D and hope that it paves the way for further, better, posts!
Ok, not the best title for my first blog post on Gama but I figured starting small would be a good introduction to my writing style (if anyone is reading this of course). So, Unity3D is, as implied by its name, a pretty good 3D game development framework. It's a shame then that I use it mainly for developing 2D games. Oh well... I guess that's a topic for a later post.
After a week or so with the engine I started noticing two things. First, I really really wanted an easy way to reset a group of game-objects to zero. And secondly, I found that creating instances of GameObject in code can be repetetive over my particular needs from them. So without further ado let me introduce answer number one: Resetting transformations using a menu item and a shortcut key (or hotkey or however you want to call these things).
The code itself is not complicated;
Notice that I added the '&z' character to the menu item's name; this causes Unity to associate the 'alt+z' shortcut to our menu and makes it easier to call it (remember: lazy is good!). And finally see that we're resetting only the local properties of the transform, this way we're avoiding a host of issues concerning the parenting hierarchy of the object.
Using this menu item a few times and writing more code for the game I also found that resetting transforms is something I need to do sometimes during Unity's runtime environment (rather than just while editing - more on my building process and separation of projects in a later post). So let's extract this into a useful utility/extension method.
Next, and last, item on the agenda for today; wrapping the GameObject creation in a useful factory method.
One of the main reasons for creating this wrapper is to allow easy parenting. Now, code!
Again, nothing overly complicated going on there but I will explain a few points. First of all, the GetAsParentTransform() method. Because the code allows parenting new instances to any component (or game object) we need to check whether that particular component is a transform (and then use it as the parent) or whether it's any other component (and then use its transform component as the parent).
The second point you (yes YOU) must have noticed is that we're adding something called a TransformLock. That's my own special sauce cooked up for Unity and, like all other things, will be the topic of another post.
Alright, until next time - so long and thanks for all the fish; also, go check out our awesome kickstarter for Buck.
Read more about:
Featured BlogsYou May Also Like