Unity-Mathf class

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Lesson1 : MonoBehaviour
{     // Start is called before the first frame update     void Start()     {         #region Knowledge Point 1 Mathf and Math         //Math is a tool class encapsulated in C# for mathematical calculations—located in the System name in space




        //Mathf is a tool structure packaged in Unity for mathematical calculations - located in the UnityEngine namespace


        //They are all #endregion         provided for math-related calculations

        #region knowledge point 2 The difference between them
        //Mathf is almost the same as the related methods in Math
        //Math is a tool class that comes with C# It mainly provides some math-related calculation methods
        //Mathf is specially packaged by Unity, not only in Math method, there are more methods suitable for game development
        //so when we develop Unity games
        //use the methods in Mathf for mathematical calculations

        #endregion

        #region knowledge point three common methods in Mathf - general calculation once
        //1.π - PI
        print(Mathf.PI);

        //2. Take the absolute value - Abs
        print(Mathf.Abs(-10));
        print(Mathf.Abs(-20));
        print(Mathf.Abs(1));
        //3. Round up - CeilToInt
        float f = 1.3f;
        int i = (int)f;
        print(i);
        print(Mathf. CeilToInt(f));
        print(Mathf. CeilToInt(1.00001f));

        //4. Round down - FloorToInt
        print(Mathf.FloorToInt(9.6f));

        //5. Clamp function - Clamp
        print(Mathf.Clamp(10, 11, 20));
        print(Mathf.Clamp(21, 11, 20));
        print(Mathf.Clamp(15, 11, 20));

        //6. Get the maximum value - Max
        print(Mathf.Max(1, 2, 3, 4, 5, 6, 7, 8));
        print(Mathf.Max(1, 2));

        //7. Get the minimum value - Min
        print(Mathf.Min(1, 2, 3, 4, 545, 6, 1123, 123));
        print(Mathf.Min(1.1f, 0.4f));

        //8. A number to the nth power - Pow
        print("A number to the nth power" + Mathf.Pow(4, 2));
        print("A number to the nth power" + Mathf.Pow(2, 3));

        //9. Rounding - RoundToInt
        print("Rounding" + Mathf.RoundToInt(1.3f));
        print("Rounding" + Mathf.RoundToInt(1.5f));

        //10. Return the square root of a number - Sqrt
        print("Return the square root of a number" + Mathf.Sqrt(4));
        print("Return the square root of a number" + Mathf.Sqrt(16));
        print(" Returns the square root of a number" + Mathf.Sqrt(64));

        //11. Determine whether a number is the nth power of 2 - IsPowerOfTwo
        print("Judge whether a number is the nth power of 2" + Mathf.IsPowerOfTwo(4));
        print("Judge whether a number is the nth power of 2 Power" + Mathf.IsPowerOfTwo(8));
        print("Judge whether a number is the nth power of 2" + Mathf.IsPowerOfTwo(3));
        print("Judge whether a number is the nth power of 2" + Mathf.IsPowerOfTwo(1));

        //12. Judgment of positive and negative numbers - Sign
        print("judgment of positive and negative numbers" + Mathf.Sign(0));
        print("judgment of positive and negative numbers" + Mathf.Sign(10));
        print("judgment of positive and negative numbers Number" + Mathf.Sign(-10));
        print("Judging positive and negative numbers" + Mathf.Sign(3));
        print("Judging positive and negative numbers" + Mathf.Sign(-2));
        #endregion
    }

    //Start value
    float start = 0;
    float result = 0;
    float time = 0;
    // Update is called once per frame
    void Update()
    {         #region Knowledge point 4 A common method in Mathf - generally non-stop calculation         // Interpolation - Lerp

        //Lerp function formula
        //result = Mathf.Lerp(start, end, t);

        //t is the interpolation coefficient, the value range is 0~1
        //result = start + (end - start)*t

       //Usage of interpolation operation 1
        //Change the value of start every frame - the speed of change is first fast and then slow, the position is infinitely close, but the end position will not be obtained
        start = Mathf.Lerp(start, 10, Time.deltaTime);

        //Usage of interpolation operation 2
        //Change the value of t every frame—the speed of change is constant, and the position is close to each frame. When t>=1, the result
        time += Time.deltaTime;
        result = Mathf.Lerp(start, 10 , time);
        #endregion

    }
}

 

Guess you like

Origin blog.csdn.net/qq_42705793/article/details/127552573