This note we focus on the various math-related classes that unity offers to learn.
Time
API Document Address: http://docs.unity3d.com/ScriptReference/Time.html
Time Plus/deceleration
DEMO04 is a free-falling elastic ball that falls from the sky and is used to test the following example.
This effect is also more commonly used in games, such as in combat games, when defeating a boss or when we die, a slow-motion effect appears.
Our script presses the space will slow down 10 times times the time, the script binds to the camera, the code is as follows:
1 usingUnityengine;2 usingSystem.Collections;3 4 Public classDemo04script:monobehaviour5 {6 voidStart ()7 {8 }9 Ten voidUpdate () One { A if(Input.getkeydown (keycode.a)) - { -Time.timescale =0.1f; theTime.fixeddeltatime =0.002f; - } - if(Input.getkeyup (keycode.a)) - { +Time.timescale =1.0f; -Time.fixeddeltatime =0.02f; + } A } at}
Note that in addition to modifying the timescale, but also to modify the physical operation of the fixeddeltatime, otherwise in the physical simulation due to the gap is too small to cause the phenomenon of jumping frames, you can comment out Fixeddeltatime code run view.
Random number
API Document Address: http://docs.unity3d.com/ScriptReference/Random.html
The random number is relatively simple, not too much to explain.
Mathematical Operations MATHF
API Document Address: http://docs.unity3d.com/ScriptReference/Mathf.html
Smooth transition discoloration
Demo05 is a small box of randomly changing colors that you can use to test the following example.
The main use of the noise in Berlin, the specific code is as follows:
1 usingUnityengine;2 usingSystem.Collections;3 4 Public classDemo05script:monobehaviour5 {6 PublicGameobject Cube;7 8 PrivateMaterial _material;9 PrivateVector3 _colorspace;Ten One voidStart () A { -_material = cube. Getcomponent<renderer>(). material; - the_colorspace =NewVector3 ( - Random.range (0f, 10f), - Random.range (0f, 10f), - Random.range (0f, 10f) + ); - } + A voidUpdate () at { - floatR =mathf.perlinnoise (Time.time, _colorspace.x); - floatg =mathf.perlinnoise (Time.time, _colorspace.y); - floatb =mathf.perlinnoise (Time.time, _colorspace.z); - -_material.color =NewColor (R, G, b); in } -}
Because Berlin noise is a smooth, random form, our color changes are also smooth transitions, and if you use normal random numbers, you don't achieve smooth transitions.
Project file Download
Please expect
Unity3d Space Conversion Learning Note (ii): Basic mathematics