Unity

Animation synchronization with soundtrack in Unity

Sometimes we might need to play the animations together with a separate soundtrack in Unity. However, animations and the soundtrack may be out of sync due to frame drop or other performance problems even we start playing them in the same frame. A simple but not efficient solution is to force the animation to play the exact frame as the soundtrack ever frame. Below is a code example.

Continue reading

Rigidbody.Constraints in Unity

We all know that physics affects the sense of reality greatly in VR development. As a developer, I always hope to make the physical behaviors of the virtual experience the same as the reality as far as possible. However, I met a problem several days ago. I created a vase with some roses in it, and they are separated Game Objects. However, during the playtest, roses in the vase “jumped out” strangely when I tried to lift the vase. I checked and rebuilt the colliders on the vase and the roses carefully, set the Collision Detection on the Rigidbody to be Continuous Dynamic, tried to bond the roses on the vase as its children, but all of them failed to erase the bug completely. However, my intuition told me that the problem must be related to the physical system, therefore must has something to do with the Rigidbody component.

Continue reading

A known, postponed Unity bug: Tab Button

Issue Tracker: https://issuetracker.unity3d.com/issues/pressing-tab-key-does-not-navigate-between-input-controls?_ga=1.166523063.801547011.1462557649

Bug Description

I was trying to implement a function when the Tab button was pressed. However, the function only responds for the first time. After the player presses Tab once, ALL hotkeys in the game being unresponsive. The only way to bring back hotkeys’ functions is to use the cursor click in the game. It happens for GetKey, GetKeyDown and GetButtonDown.

Noticed that the problem only occurs under editor mode and development builds. It disappears in release builds (none-development builds).

I’ve also checked EditorWindow.focusedWindow and EventSystem.current.currentSelectedGameObject. They are all correct when the problem occurs.

The bug has been marked as a “Postponed Issue” by the Unity folks, where “Postponed Issue” means:

Postponed means that the issue was either a feature request or something that requires major refactoring on our side. Since that makes the issue not actionable in the close future we choose to close it as Postponed and add it on our internal roadmaps and technical debt pages instead.

 

Limit Max Width of a Layout Component in Unity

Original post of the problem: http://forum.unity3d.com/threads/limit-max-width-of-layout-component.316625/

Demand

A textbox with a child text component. The textbox width would increase with the length of the text up to this maximum value at which point the width would be fixed forcing the text to wrap to the next line now increasing the height of the button.

A common needed of UI. I was surprised that Unity didn’t have max width for their vertical layout component so far… But never mind, using combinations of vertical and horizontal layouts could achieve what we want.

Continue reading

Bitfields for multiple flags in Unity

Demand

Supposed we’re going to develop a shooting game. Characters in our game have several flags such as IsMoving, IsShooting, IsLoading, IsHiding… All these behaviors might happen simultaneously (or, of course, individually), say, the character can shoot while moving, can load his gun while hiding. How are we going to set these flags?

The simplest way might be allocating bools for them respectively. However, bools in C# are stored as ints and the compiler might not be able to combines/consolidates them. So if there’re 16 bools, that’s an allocation of 16 ints – wasteful. In addition, if one of those combinations (eg. IsMoving + IsShooting) are frequently used, the codes might be burden.

Following are some tips for such situation.

Continue reading

Talking about time.deltaTime

For a long time, I didn’t quite understand what is Time.deltaTime, and why we have to use it when we want to move an object smoothly. I had used it for many times, but never understood its meaning.

Today, after I had a big trouble when writing a script of the player’s operation, I decided to find out what it is. I wrote a coroutine function to detect the input of the players. However, sometime the input may be missed in the function, while being able to be detected in the Update() function. To loop the coroutine function I used the line, which seemed like the core of the problem:
Continue reading