Unity之Math等方法的使用
生活随笔
收集整理的這篇文章主要介紹了
Unity之Math等方法的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Math方法
其實內容是比較簡單的,只是筆者想想養成記筆記的習慣,所以我們直接看代碼。
using System.Collections; using System.Collections.Generic; using UnityEngine;public class MaTh : MonoBehaviour {// Start is called before the first frame updatevoid 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 framevoid 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 updatevoid 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 framevoid Update(){} }小結 想漸漸的找回當初認真讀書的感覺,一點一點的積累。一起加油吧!
總結
以上是生活随笔為你收集整理的Unity之Math等方法的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#学习小结(DAY1)
- 下一篇: Input类,Vector3实例