Unity tips / personal guidelines [work in progress]
I have started with Unity development. This page sumarizes some things I have used and I maybe will use them again in another project. This page is
to remember them and found them easily, there is no pattern or order in presented things.
Some useful tutorials
First tutorial project - pdf (Based on video tutorial from Unity3D: source)
Shaders
Physics
Combinations of collision responses based on body / collider settings
SphereCast
SphereCast moves a Sphere volume down the direction and distance of a ray and detects what it bumps into (source).
Force modes
- ForceMode.Force. If the AddForce call occurs in a FixedUpdate loop, the full force supplied to the AddForce call will only have been exerted on the rigidbody after one second. Think of it as 'Force exerted per second'
- ForceMode.Acceleration. Like ForceMode.Force, except the object's mass is ignored. The resulting movement will be as though the object has a mass of 1. The following lines will give the same result
rigidbody.AddForce((Vector3.forward * 10),ForceMode.Force);
rigidbody.AddForce((Vector3.forward * 10)/rigidbody.mass,ForceMode.Acceleration);
- ForceMode.Impulse. The entirety of the force vector supplied to the AddForce call will be applied all at once, immediately.
- ForceMode.VelocityChange. Like ForceMode.Impulse except the object's mass is ignored. So the following lines give the same result:
rigidbody.AddForce((Vector3.forward * 10),ForceMode.Impulse);
rigidbody.AddForce((Vector3.forward * 10)/rigidbody.mass,ForceMode.VelocityChange);
Copied from: source.
Scripts
Links
Execution order of Unity event methods - also gives list of those methods and what they do
GUI
World space canvas
Use RectTransform to set Width / Height in pixels. This is NOT the real size we want to see. If we want our canvas to be 40x20, use 400x200 and set Scale to be (0.1, 0.1, 0.1).
If 40x20 is set directly without use of scale, children elements such Fonts, Images etc. will be too small or result in smudged textures. I don't know why, but this was the only
way it worked correctly for me (with scale).
World space GUI rendered always on top
To render world space based GUI on top of everything, use two cameras.
- First - renders world. Set Culling mask - Everything without GUI
- Second - renders world space GUI (and possibly any other GUI elements). Set Culling mask - UI only, Clear flags - Depth only. Also, maybe use Depth set to 100 (maximal value).
Otherwise, first and second camera have the same settings. That means, if render to texture is use, both cameras must have the same texture attached.
In scene hierarchy, second camera must be after the first one.
Other solutions / sources - link (solution from debeani is described above).
Other
Final build
In my code, I have used
Shader.Find
. Those shaders must be in Resources folder!!! If they are not, it will work in editor, but not in build (source).
Disable frame lock
Edit -> Project Settings -> Quality: The default quality profile is "Good", so expand "Good", and set "VSYNC Count" to "Don't Sync. Also, sometimes its needed to put Application.targetFrameRate = -1; somewhere in a script. (source).
Martin Prantl 2015