Unity之Math等方法的使用

Math方法

其实内容是比较简单的,只是笔者想想养成记笔记的习惯,所以我们直接看代码。

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

public class MaTh : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log(Mathf.Lerp(1.0f, 3.0f, 0.5f));//按百分比取值,打印出的应该是2.0、

        Debug.Log(Mathf.Clamp(1, 3, 5));//(int value,int min,int max)

        Debug.Log(Mathf.Max(1, 10));//打印最大值;

        Debug.Log(Mathf.Min(1, 10));//打印最小值;

        Debug.Log(Mathf.Abs(-3));//取绝对值

        Debug.Log(Mathf.Sin(20));//按弧度取得;


    }

    // Update is called once per frame
    void Update()
    {
        
    }
    //Awake --> Start --> Update --> FixedUpdate --> LateUpdate -->OnGUI -->Reset --> OnDisable -->OnDestroy  Unity脚本函数执行顺序
}

**(1)**Mathf.Lerp方法

Debug.Log(Mathf.Lerp(1.0f, 3.0f, 0.5f));

第一个参数和第二个参数是范围,返回的是前两个参数按第三个参数百分比的取值,也即:返回值=(参数二-参数一)*参数三;

**(2)**Mathf.Clamp方法

Debug.Log(Mathf.Clamp(1, 3, 5))

第一个参数为返回值,参数二与参数三为范围,如果第一个参数在范围内,则返回,如果小于第一个参数则返回参数二,大于第三个参数则返回参数三。

Random方法

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

public class RanDom : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        int a = Random.Range(0, 10);//随机取值在0~10之间。
        float b = Random.Range(0.0f, 0.9f);//随机取值在0与0.9之间

        Debug.Log(a);
        Debug.Log(b);
    }

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

小结 想渐渐的找回当初认真读书的感觉,一点一点的积累。一起加油吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值