1擒悬、Mathf.Clamp
設(shè)置指定參數(shù)數(shù)值的范圍崩哩,在最大值和最小值之間。
public static float Clamp(float value, float min, float max)
Description
Clamps a value between a minimum float and maximum float value.
using UnityEngine;using System.Collections;
public class ExampleClass :MonoBehaviour{? ?
? ? void Update() {? ? ? ?
? ? transform.position = new?Vector3(Mathf.Clamp(Time.time, 1.0F, 3.0F), 0, 0);? ?
? ? ?}
}
public static int Clamp(int value, int min, int max);
Description
Clamps value between min and max and returns value.
// Clamps the value 10 to be between 1 and 3.
// prints 3 to the console
Debug.Log(Mathf.Clamp(10, 1, 3));
2烹俗、定義一個(gè)公共調(diào)用類并序列化
公共類之前要注意做序列化[System.Serializable]
[System.Serializable]
public class Boundary?
{
? ? public float xMin, xMax, zMin, zMax;
}
調(diào)用時(shí):
先聲明public Boundary boundary; 再調(diào)用boundary.xMin
3禁荸、用歐拉角來改變剛體旋轉(zhuǎn)角度
rigidbody.rotation = Quaternion.Euler(0.0f,0.0f,rigidbody.velocity.x * tilt);
4、協(xié)同程序StartCoroutine
yield return new WaitForSeconds(time)
這給方法就是讓代碼等待一定時(shí)間葫录,這是個(gè)協(xié)同程序着裹,可以讓游戲不暫停的同時(shí),讓代碼暫停.
using UnityEngine;using System.Collections;
public class WaitForSecondsExample :MonoBehaviour{ ? ? ?
void Start() { ? ? ? ?
StartCoroutine(Example()); ??
?}? ? ? ?
?IEnumerator Example() {? ? ? ?
? ? ?print(Time.time);? ? ? ??
? ? ?yield return new?WaitForSeconds(5);? ? ? ?
? ? ?print(Time.time);? ?
}? ?
}