Unity Mathf Class

Unity 学习笔记汇总
Mathf官方API使用文档
Vector3官方API使用文档

1. Mathf’s Clamp static method

1.1. public static float Clamp(float value, float min, float max);

Parameters

value The floating point value to restrict inside the range defined by the min and max values.
min The minimum floating point value to compare against.
max The maximum floating point value to compare against.

Returns
float The float result between the min and max values.

Description
Clamps the given value between the given minimum float and maximum float values. Returns the given value if it is within the min and max range.

1.2. public static int Clamp(int value, int min, int max);

Parameters

value The integer point value to restrict inside the min-to-max range
min TThe minimum integer point value to compare against.
max The maximum integer point value to compare against.

Returns
float The int result between the min and max values.
Description
Clamps the given value between a range defined by the given minimum integer and maximum integer values. Returns the given value if it is within min and max.

Returns the min value if the given value is less than the min value. Returns the max value if the given value is greater than the max value. The min and max parameters are inclusive. For example, Clamp(10, 0, 5) will return a maximum argument of 5 and not 4.

1.3. Test

1.3.1. Foreground

Create a 3D object Cube and a test C# script, then bind the 3D object Cube to a variable that named cube in the test C# script.
在这里插入图片描述

1.3.2. Code

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

public class test : MonoBehaviour
{
    public GameObject cube;
    float a = 10;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float x = Input.GetAxis("Horizontal");//-1~1

        //When we press A or D button, the cube will turn red;
        if (Mathf.Abs(x)>0)
        {
            cube.GetComponent<Renderer>().material.color = Color.red;
        }
        else
        {
            cube.GetComponent<Renderer>().material.color = Color.green;
        }

        //The value of a goes from 9.8 to 0. 
        a = Mathf.Clamp(a - 0.2f, 0, Mathf.Infinity);
        Debug.Log(a);

        //Cube will move as the value of a changes.
        //cube.transform.Translate(new Vector3(a, 0, 0) * Time.deltaTime);
        cube.transform.position = new Vector3(a, 0, 0);
    }
}

1.3.3. Result

  • When we press A or D button, the cube will turn red;
    在这里插入图片描述
  • The value of a goes from 9.8 to 0.
    在这里插入图片描述
  • Cube will move as the value of a changes.
    在这里插入图片描述

2. Mathf’s Lerp static method

2.1. public static float Lerp(float a, float b, float t);

Parameters

a The start value.
b The end value.
t The interpolation value between the two floats.

Returns
float The interpolated float result between the two float values.

Description

  • Linearly interpolates between a and b by t.
  • The parameter t is clamped to the range [0, 1].
  • When t = 0 return a.
    When t = 1 return b.
    When t = 0.5 return the midpoint of a and b.

Example of usage

  • For Mathf.Lerp(1, 2, t):
    When t ≤ 0 return 1.
    When t ≥ 1 return 2.
    When t = 0.5 return the midpoint of a and b.
    When 0<t<0.5, returned value closer to 1.
    When 0.5<t<1, returned value closer to 2.

    The code to do this is shown below.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class test : MonoBehaviour
    {
        float a = 0, t = -1;
        // Start is called before the first frame update
        void Start()
        {
            
        }
    
        // Update is called once per frame
        void Update()
        {
            /*
             For Mathf.Lerp(1, 2, t):
                When t ≤ 0 return 1.
                When t ≥ 1 return 2.
                When t = 0.5 return the midpoint of a and b.
                When 0<t<0.5, returned value closer to 1.
                When 0.5<t<1, returned value closer to 2.
                */
            Debug.Log("" + Mathf.Lerp(1, 2, t) + ", " + t);
            t += 0.01f;
        }
    }
    
    

3. Homework

Sin Returns the sine of angle f.
Sqrt Returns square root of f.
Pow Returns f raised to power p.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        print("Mathf.Sin(10)=" + Mathf.Sin(10));
        print("Mathf.Sqrt(9)=" + Mathf.Sqrt(9));
        print("Mathf.Pow(10, 2)=" + Mathf.Pow(10, 2));
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读