I’ve spent the last couple of sessions working out a camera system to use for “All Aboard!”. I had a few requirements in mind:
- Must be able to pan, zoom and rotate
- Must be able to clamp to world size and not move past boundaries
- Must be both desktop and touch compatible
- Must be easy to augment/expand
I spent some time looking through tutorials and samples and they got me relatively close to what I needed, but there were some foundational functions that became more difficult to implement than imagined, so I started looking through the asset store because this has most definitely been solved before, and why re-invent the wheel?
I came across this free RTS camera on the asset store, and while it does perform nicely, it was missing a lot of what I needed. So, I could continue with my own code, try to augment this free camera asset, or look for something better. So, I kept looking.
Then I found the Mobile Touch Camera asset, which is absolutely perfect for what I’m building. It hits all four of the requirements listed above, and is relatively simple to augment and expand for additional features. So, credit goes out to BitBender games for the incredible asset! If you are looking for a solid camera system for your game, and need mobile/touch support, I highly recommend this asset!
Requirement #1: Must be able to pan, zoom and rotate — Check!
Requirement #2: Must be able to clamp to world size and not move past boundaries – Check!
Requirement #3: Must be both desktop and touch compatible – Check!
Requirement #4: Must be easy to augment/expand – Check!
The camera already comes with the ability to select and move items on the screen, which woks like a charm. There were a few more things that I needed to add to it to make it fully functional:
- Collision detection and preventing items from overlapping
- Rotation of the items once selected (and the ability to show a separate UI when an item is selected)
- Ability to have items that have collision detection warnings, but are unable to move themselves (static buildings, roads, etc)
The collision detection was something that I spent more of my time on than I had wanted to do. In the end, it was as easy as adding a BoxCollider and RigidBody with static coordinates. Then, I check for collisions when the piece is moved on screen. If a collision is found, the piece will turn red. If the player tries to place it down in an invalid area, it will snap back to its original position.
Rotation was a little trickier because I had to prevent the UI click-throughs and auto-deselect of the items. When the player selects a piece, the toolsUI will show (currently just a rotate button). The piece remains selected until explicitly deselected, so the user is free to tap around the UI and manipulate the item. Additionally with rotation, I had to make sure that if a player rotated an item that ended up overlapping something else, the collision detection scripts took care of it.
The third piece was much easier. The script will loop through two Lists, one consisting of all pickable items, the other consisting of all non-pickable items. Both pickable and non-pickable items contain a RigidBody and BoxCollider, so simply looping through both lists gives me what the item is colliding with.
This is all working great, and I couldn’t be happier with the decision to implement this into All Aboard!