Showing results for 
Search instead for 
Did you mean: 
Honored Contributor III

Unity Tip: Creating A Virtuous Circle Of Lag Reduction With RealSense and C#

Hi everyone,

You may remember that in past articles, I have recommended amplifying the movement of RealSense-powered objects in Unity by creating a chain of childed objects, each with a single TrackingAction script with an identical configuration.  By doing this, Unity builds on the movement effort generated by the TrackingAction in the object above it in the hierarchy to generate a 'gearing ratio' effect, where the more TrackingAction' gears' you have in an object chain, the greater the movement of the overall object.

Whilst this method is extremely effective, it does come at a cost.  You may find that the computer's memory resources are being drained faster by the multiple simultaneously active copies of the TrackingAction, for example.  This means that you have rapid speed in the short term but the entire application begins t slow down after time.

I have found a much better approach to speed amplification though, and it is the C# scripting language.

In Unity, the two main supported scripting languages are JavaScript and C#.  JavaScript is easy to learn and user friendly, but it can be slower than C#.  In an ideal world, one would choose to always choose to write scripts in C# instead of JavaScript because of the speed advantage.  However, C# is a less friendly and less forgiving language than JavaScript and can look completely daunting to someone who hasn't used it before.  Part of the reason for this is because C# scripts tend to be three times the length of a JavaScript that performs the same function, and so it looks very impenetrable even when just looking at it, never mind attempting to actually learn it.

It seemed as though the only way that I was going to overcome difficulties that I had in my project (caused by JavsScripts that were designed to affect the C# Action scripts not running fast enough) was going to be to force myself to try to learn C# and re-write my JavaScripts in that language.  So I dedicated a few days to learning the basic rules and structure, and found that it actually wasn't nearly as difficult as it looked like it was going to be.  

If you have had experience with JavaScript then it is not that hard to adapt to C# in Unity, since the instructions tend to be very similar, or just use different words to describe a function (such as 'void' instead of 'function' and 'public' instead of 'var.  For instance, I was able to re-use a JavaScript that rotated an object simply by changing 'function Start()' to 'void Start().'

By starting with converting very simple scripts with one or two lines, I gained confidence and experience in C# quickly.  And after a week, I had converted most of my project's code to C#.  C# also allowed me to greatly optimize the project's scripting by making direct activation and deactivation calls to other scripts ("hitting the metal") instead of needing the large amounts of middle-man scripts that drained the project's processing resources.  And the change in the project's speed was remarkable.

Through the optimization that C# provided, I found that my RealSense-powered full body avatar was moving much faster - uncontrollably so, in fact.  This was because when I was using JavaScript, I had to compensate for slowness by installing amplification measures for the TrackingActions.  With the lag now gone, those amplifications were making the RealSense controls -too- fast.  

I set about analyzing my avatar and finding where I could remove additional TrackingActions without negatively impacting the motion of the objects they were within (i.e having them go from being too fast to being too slow).  At the end of this process, I was able to remove ten now-unneeded TrackingActions from the avatar.  

Th major benefit of doing so was that although I had reduced the number of TrackingActions, in doing so I had eliminated the lag generated by those multiple scripts.  This meant that whilst in theory the object speed should have been significantly reduced, in practice the reduction was barely noticable because the overall processing speed of the RealSense camera inputs had sped up in response to the elimination of those ten TrackingActions.

Another benefit was that the reduction in TrackingActions made possible by writing non-RealSense scripts in faster C also eliminated their drain on project resources.  This made the application not only more stable in the immediate term but also made it more stable for longer running periods.

In summary, I strongly recommend that anyone starting a new Unity project goes to the trouble of learning C# and using it right from the beginning, as it has a major beneficial effect on RealSense projects.  And if you have already begun using JavaScript, I really recommend going to the trouble of learning C#, because the advantages can be huge.  And as Unity is said to be moving towards a focus on C# in the future instead of JavaScript, you'll be future-proofing yourself and creating code that can be re-used in subsequent projects.

Using C# makes your project run faster overall.

This means that you can afford to use less TrackingActions.

Using less Trackingactions makes a project faster.

This is the Virtuous Circle referred to in the article title!

A good place to start learning the structures of C# may be the article below, where I look at a C# script and break it down section by section to explain it.

Best of luck!   If you have any questions, please feel free to ask them in the comments!

0 Kudos
0 Replies