Unity3d Space Conversion Learning Note (ii): Basic mathematics

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.