Project: Gauntlet - Current Iteration

Thursday, December 17, 2009

Day Z: Raycast performance, Unity Profiler, unavoidable performance issues and damage mitigation.

As our giant robot rolls along - to uncertain horizons, his head-mounted laser moves too - relative to his skull.

Now, the aim is - to avoid this laser, whether it be by hiding behind a building, a car etc.

Technically - the laser is a basic primitive (a cylinder or cube) - with an Additive Particle shader that gives it the glowing/half transparent appearance.

The issue is this:

If you are hiding behind an obstacle, the laser will appear to come through this obstacle - which would negate the point of hiding.

As the laser is not a light source or physics object, the way I am assuring this does not happen is as follows:

  1. From the parent rotation object (the skull) cast a ray along the axis of the primitive (the laser).
  2. If the raycast strikes a collider, find the distance to this collider.
    1. Scale the laser to equal this distance.
      1. Assuring the laser has a starting scale of 0 along the length axis.
    2. Offset the laser by half of this distance.
      1. As primitives in unity begin with an origin directly in their centre.
  3. Presto, your laser is not penetrating the obstacle.
Now this is fine and functional, but if we refer to our beautiful unity profiler, you see that each raycast is causing a noticeable drop in framerate.

As our laser does not have to be 100% accurate, I have offset each check for boundaries (parametrically).

In a very primitive fashion....

Each listener has a counter, when the counter hits the defined check rate, it will check for a collision.

We mitigate the average damage, while still taking damage at the predefined rate.

The aim is to use this concept over all performance-detrimental tasks (collision detection, raycasting, etc), and offset the counters to assure that not all checks are occurring in one enormous spike.


A glimpse of my hilarious test scene, replete with a random cube (right), an orb flashing at 120bmp (center), a wall and the ray - white. As you can see, the ray does not penetrate the wall.

No comments: