Tuesday, April 16, 2013

Tips and tricks collected for Unity3D game development #1

Hey everyone,
I've decided to write a post containing random tips and tricks I've found along the way for Unity3D development.

As a software engineer for over 14 years, I like to break apart each new development environment I get into - and Unity wasn't any different :)

So, here goes:


Dynamic, static and kinematic colliders

There are 3 types of colliders in Unity3D:
  1. Dynamic colliders - Used for collision detection for dynamic objects (enemies, bullets, boulders, etc) - the setup is pretty straightforward, a rigidbody is attached to a collider
  2. Static colliders - Used for collisions for anything that never moves in your level (level floors, walls, obstacles, etc) - these should only contain a collider without a rigidbody.
  3. Kinematic colliders - Used for collisions for anything that moves using code and not physics (moving platforms, elevators, etc) - these must have a rigidbody marked as kinematic with a collider.
Adding kinematic rigidbodies to kinematic colliders greatly improves performance  as moving static colliders around causes internal calculations every time you move them.
Kinematic rigidbodies solve this problem.

Everything that doesn't move, rotate or scale should always be marked static

This tells Unity that the object can be optimized for static batching - this greatly lowers active draw calls and removes a lot of calculations from the CPU.

Use Handles, Gizmos and Custom Editors

Gizmos are great to show custom logic inside the editor - for example, show the range an AI unit can spot the player (using Gizmos.DrawWireSphere for example), draw a movement path (DrawLine) and countless other scenerios.

Handles are terrific to help you edit custom properties visually on your game objects.
For example, using Handles.PositionHandle to move around a target, RadiusHandle to edit a radius for an AI unit, etc.

Custom Editors will make your life incredibly easier when used on medium-large scale projects and the possibilites to using them are endless.

MonoBehaviour's built-in properties

Recently I've used Unity's Pro profiler, and changing a class that accessed MonoBehaviour.transform about 3 times every frame - calling MonoBehaviour.transform/collider/renderer/etc is equivellant to using MonoBehaviour.GetComponent - which isn't too efficient if you use them often.

Instead, define a private property inside your class, store the required component there, and later access it directly.

Doing this, dramatically improved frame cost for that class.

In general: Don't use Unity's GUI functions

Unity's GUI functions are very costly and should be avoided at all costs.
Instead, use a 2D framework (NGUITK2DEX2D, etc) that projects all GUI into a 2nd camera, which has a higher depth and has it's clear flags set to none.

This way you will enjoy the benefits of dynamic batching, higher performance and scalable GUI on an orthographic camera.

---

So that's it for today, I hope my tips will help someone out there.
Drop a comment if you have any ideas, feedback or find any mistakes that I've posted.

Cheers.

4 comments:

  1. The existing module, Unity 3D Games which long ago cost $99 for an individual permit and $249 for an expert one, will soon be accessible free of charge on Microsoft's download website. Syntaxtree says it will additionally soon contact its current clients to give them with further subtle elements to how it will keep on supporting them. The rundown of Unityvs' present clients incorporates any semblance of Microsoft Studios, Electronic Arts, Blizzard, Valve and Rovio.

    ReplyDelete
  2. Hi ,
    My Game is Running Game Same As AngeryRun.I am generate radonm abstracles.
    every abstracles having one tag.now game play is start first generate abstracts is player is colliding game over popup is not coming.after three or fourth abstract is colliding game over popup is comming .
    pls tell me any solution for me.

    ReplyDelete
  3. I liked your article and I hope you will have many entries or more.
    Mobile Application Developer USA

    ReplyDelete