0%

Basic Skills

  • Usually set the values of private values in the Start() function.
  • Only gameObject has the function of GetComponent
  • Transform
    • Only transform has the function of Find(string name)
    • The function of Find(string name) for a transform can only find the direct children of a transform

Physics Engine

Collider

Wheel Collider

Wheel collider is a collider rather than the mesh of the wheel. You can add force or torque to the collider to make the collider rotate like a wheel.

Rigidbody

Rigidbody has a element named velocity, which can set the speed(Vector3) of the gameobject.

Detect collision or collision position between objects

  • Use OnCollisionEnter or OnTriggerEnter
    • Required object to have collider and rigidbody
  • Use Physics.OverlapSphere(point) to detect whether the point collides with other colliders.
  • Use Collider.Raycast to get the accurate collision position. It will ignore other colliders but this collider.

Scripting

Find Objects

  • Use transform.root.gameObject can find the root object
  • Use gameObject.GetComponentInChild() can find the script attached to the child objects.
  • Use transform.Find() can find the children objects, but this is only used to find the direct children objects.

Attributes

Attributes of C# can add metadata (information about the types defined in a program) to the program. In Unity, it can be used to show information in Unity editor’s inspector.

The attributes work by placing the name of the attribute enclosed in square brackets ([]) above the declaration of the entity to which it applies. There are several different attributes in Unity

Name Function
HideInInspector Hide public parameter in Unity Inspector
SerializeField Show the private parameter in Unity Inspector
Space Show a space line in Unity Inspector
Header(“XX”) Show a comment for the parameter in Unity Inspector
Range(min, max) Show the value range in Unity Inspector

Asynchronous

Unity can use async/await keywords of C# to achieve asynchronous methods, which can be found here.

However, some Unity library functions cannot be executed correctly in the asynchronous thread. Instead, Unity has its own asynchronous keyword, which is IEnumerator/yield return.

yield return keyword has many kinds of return type, which shows following

Type Function
yield return new WaitForEndofFrame() Wait until the end of current frame
yield return new WaitForFixedFrame() Wait until next frame
yield return new WaitForSeconds(xx) Wait for xx seconds
yield return new WaitForWhile(func) Wait until func return false
yield return new WaitForUntil(func) Wait until func return true

In Unity scripts, sometimes we need to do some actions after changing the properties of the GameObject, e.g. position, rotation. Unity will finish the changing action in the end of current frame. So we need to wait for a while before doing other actions.

Note. the event of delegate trigger is also finished in the current frame.

Also in Unity, we need to use yield return null or allow the code return to the main thread to do some changes in the scene.

Note. don’t use yield return 0 because it is the same with yield return null but it will lead to boxing action.

Compile

Conditional Compile

Unity has many targeted platforms, so the codes can be conditional complied according to this link

Compile Pipeline

  • Unity will compile C# scripts to IL with C# compiler firstly
  • Unity will also combines all the other assemblies like the UnityEngine.dll and mscorlib.dll
  • Unity will use Mono or IL2CPP to build the IL scripts to naive code

Git is a powerful version management system. This posts captures the common used commands.

Read more »

Noise exists in the world, ans also exists in the robot motion and observations, so robots never know exactaly of the environment and themselves. However, probality theory could represent the uncertainty and give a better guess or estimation of the world. Therefore, the state estimation is very important in Robotics.

Read more »

Import a personal lib

Importing the .ipynb file we wrote as a module to a new .ipynb file is different from importing them in a .py file. The easiest way is to convert those .ipynb file to a .py file and you can import those in a new .ipynb file. However, this method doesn’t allow you to modify the code and reload it easily.

When the .ipynb lib file is in the same folder with you

For example, the lib’s name is test.ipynb. There is a function called hello() within it. In a new .ipynb file, you need to type the following.

1
2
3
4
5
6
7
# !pip install import-ipynb
import import_ipynb
import importlib
importlib.reload(test)
import test

test.hello()

The import_ipynb lib is allowing you to import a .ipynb file. The importlib file is allowing you to reload the module thus the new change to the lib file can be reloaded into your file.

When the .ipynb lib file is in the subfolder of current folder

For example, the lib file in the folder of lib. The lib’s name is test.ipynb. There is a function called hello() within it. In a new .ipynb file, you need to type the following.

Powershell is a more powerfull shell than CMD in Windows. However, the layout of powershell in Windows is very ugle, especially the default font. In order to change the font of powershell, we need to do several works.

Descipline

Actually the font can be change in the powershell if we click the top left corner of the powreshell interface. While if the CodePage is 936(Chinese), you can only change the font of powershell among several Chineses fonts, which are not well performed in the powershell.

So we need to find some way to change the CodePage of powershell to 437(English), then we can change the font to some English font like Consolas.

In order to change the default page to another, there are two steps need to finish that. One is for the powershell launched by Win+R, and another is the powershell launched by the start menu. It is because the software launched by shortcut follows other disciplines which I don’t know much.

Change for the Win+R powershell

  1. Open the Win+R, and input regedit
  2. Navigate to \HKEY_CURRENT_USER\Console\%SystemRoot%_System32_WindowsPowerShell_v1.0_powershell.exe
  3. Create/Change the CodePage of type DWORD to decimal 437

Change for the shortcut powershell

  1. Right click the powershell in start menu and go to the file location
  2. Right clikc the ico and go the properties
  3. Add the following words to the end

-NoExit -Command "chcp 437; cls"

The command above means the powershell will run the two commands automatically. The chcp 437 means to swtich the Codepage to 437; and the cls means to clear the powershell.

Environment and Packages Management

For environment, you can use virtualenv to create a virtual python environment to seperate this to the original environment.

1
virtualenv XX

Note. the virtual env folder will be located to your current folder of the bash locates. When you change the path of the env folder, it will cause problem for some direct running module. Like pip, jupyter

For packages, you can use the following command to export installed packages in env1 and install those packages in env2

1
2
(env1) $ pip freeze > requirement.txt
(env2) $ pip install -r requirement.text