Unity API常用方法和类学习笔记2
------Mathf & Input & Vector & Random
类Mathf
一、静态变量
print(Mathf.Deg2Rad);//角度转变为弧度
print(Mathf.Rad2Deg);
print(Mathf.Infinity);//无穷大
print(Mathf.NegativeInfinity);//负无穷大
print(Mathf.PI);
print(Mathf.Epsilon);//无穷小
二、静态方法
public Transform cube;
public int a = 8;//t=0
public int b = 20;//t=1
public float t = 0;
public float speed;
void Start () {
//2、简单运算方法
//取离序列2 4 8 16 32 ......中最近的序列中的数
print(Mathf.ClosestPowerOfTwo(2));//2
print(Mathf.ClosestPowerOfTwo(30));//32
print(Mathf.Max(1, 2));//2
print(Mathf.Max(1, 2, 7, 3, 10));//10
print(Mathf.Min(5, 1, 2));//1
print(Mathf.Pow(4, 3));64
print(Mathf.Sqrt(4));//2
cube.position = new Vector3(0, 0, 0);
}
void Update () {
//1、Clamp——界定某变量的值
cube.position = new Vector3(Mathf.Clamp(Time.time, 1.0f, 3.0f), 0, 0);
Debug.Log(Mathf.Clamp(Time.time, 1.0f, 3.0f));
//3、重要运算方法
//(1)Lerp
//按t的比例取a到b之间的(插)值;t=0时,值为a;t=1时,值为b
print(Mathf.Lerp(a, b, t));
//(2)MoveTowards
print(Mathf.MoveTowards(a, b, t));//new value = a + t;不会超过b;t为负时,远离目标位置b
//(3)PingPong
print(Mathf.PingPong(t, 20));//在t到20之间来回运动
//应用举例(控制物体运动)
//特点:先快后慢(距离目标位置越来越近,newX与目标点的距离 * 比例t就会越来越小)
float x = cube.position.x;
//float newX = Mathf.Lerp(x, 10, Time.deltaTime);
float newX = Mathf.MoveTowards(x, 10, Time.deltaTime);//匀速运动,-Time.deltaTime远离目标
//cube.position = new Vector3(newX, 0, 0);
cube.position = new Vector3(5 + Mathf.PingPong(Time.deltaTime * speed, 5), 0, 0);//5到10之间运动
}
//实际运用举例
private int hp = 100;
void TakeDamage()
{
hp -= 9;
//if (hp < 0)
// hp = 0;
hp = Mathf.Clamp(hp, 0, 100);
}
类Input
一、静态方法
1、keycode & keys
keycode
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Down");//按下时触发
}
if (Input.GetKeyUp(KeyCode.Space))
{
Debug.Log("Up");//抬起时触发
}
if (Input.GetKey(KeyCode.Space))
{
Debug.Log("Key");//按着时之后触发
}
}
keys
2、鼠标按键:0 for left button, 1 for right button, 2 for the middle button
(1)一直保持单击按下就一直触发
if (Input.GetMouseButton(0))
Debug.Log("Pressed left click.");
if (Input.GetMouseButton(1))
Debug.Log("Pressed right click.");
if (Input.GetMouseButton(2))
Debug.Log("Pressed middle click.");
(2)只执行一次
if (Input.GetMouseButtonDown(0))
Debug.Log("Pressed left click.");
if (Input.GetMouseButtonDown(1))
Debug.Log("Pressed right click.");
if (Input.GetMouseButtonDown(2))
Debug.Log("Pressed middle click.");
3、Virtual Axes–优点:形象、可自定义、可设置多个按键触发
(1)Button
if (Input.GetButtonDown("Fire1"))
{
print("Fire1 Down");
}
(2)Axis
试监测
if (Input.GetButtonDown("Horizontal"))
{
print("Horizontal Down");
}
常用虚拟轴及其使用方法
public Transform cube;
void Update () {
//常用虚拟轴使用方法
//加速效果
cube.Translate(Vector3.right * Time.deltaTime * Input.GetAxis("Horizontal"));
//无加速效果(按下立马返回1)--监测灵敏
cube.Translate(Vector3.right * Time.deltaTime * Input.GetAxisRaw("Horizontal"));
}
二、静态变量
1、anyKeyDown
if (Input.anyKeyDown)
{
print("Any Key Down");
}
2、mousePosition–监测鼠标的位置坐标
print(Input.mousePosition);//以屏幕左下角为原点,以像素为单位,z轴恒为零
三、拓展:结构体Touch及触摸相关常用变量和方法
1、类Input中Touch相关静态变量
2、类Input中Touch相关静态方法
3、结构体Touch中常用静态变量
4、Phase-TouchPhases触摸状态一览
结构体Struct Vector2
可与Vector3互相转换,用法也基本一致
一、静态变量
print(Vector2.down);
print(Vector2.up);
print(Vector2.left);
print(Vector2.right);
print(Vector2.one);//(1,1)
print(Vector2.zero);//(0,0)
二、构造函数
Vector2 a = new Vector2(2, 2);
Vector2 b = new Vector2(3, 4);
三、普通变量
print(a.magnitude);//计算向量长度
print(a.sqrMagnitude);//计算未开根号的长度:x^2+y^2
print(b.magnitude);
print(b.sqrMagnitude);
print(a.normalized);//取向量的单位向量,对向量本身没有影响
print(b.normalized);
print(a.x + "," + a.y);//输出向量的横纵坐标值
print(a[0] + "," + a[1]);//this[int]--this[0]==a.x; this[1]==a.y
四、作为结构体中变量与作为类中变量的修改对比
transform.position = new Vector3(3, 3, 3);
//transform.position.x = 10;//若为类,position为引用类型,即可用对象.变量直接修改
Vector3 pos = transform.position;
pos.x = 10;
transform.position = pos;//对于结构体,position为值类型,需要整体赋值从而进行修改
五、公有方法
Vector2 a = new Vector2(2, 2);
Vector2 b = new Vector2(3, 4);
print(a.x + "," + a.y);//(2,2)
a.Normalize();//向量的单位化,改变了向量本身,无返回值
print(a[0] + "," + a[1]);//(0.707,0.707)
六、静态方法
void Start () {
Vector2 a = new Vector2(2, 2);
Vector2 b = new Vector2(3, 4);
Vector2 c = new Vector2(3, 0);
print(Vector2.Angle(a, c));//计算两向量的夹角
print(Vector2.ClampMagnitude(c, 2));//(2,0)保持向量方向不变,限定向量长度
print(Vector2.Distance(b, c));
//按长度比例t分别取两向量位置值(x和、y和)的插值且仅能取a、b之间的值
print(Vector2.Lerp(a, b, 0.5f));//(2.5,3)
print(Vector2.Lerp(a, b, 2f));//b(3,4)
//按长度比例t分别取两向量位置值(x和、y和)的插值且不限定范围--保持方向按比例t取坐标
print(Vector2.LerpUnclamped(a, b, 0.5f));//(2.5,3)
print(Vector2.LerpUnclamped(a, b, 2f));//(4,6)
print(Vector2.Max(a, b));//(3,4)
print(Vector2.Min(a, b));//(2,2)
//另:Vector3---Slerp为按角度比例t取两向量的插值
}
public Vector2 a = new Vector2(2, 2);
public Vector2 target = new Vector2(10, 3);
void Update () {
a = Vector2.MoveTowards(a, target, Time.deltaTime);//按Time.deltaTime的速度即1米1秒由a位置运动到target位置
}
Vector3静态方法
七、Operators运算
Vector2 a = new Vector2(2, 2);
Vector2 b = new Vector2(3, 4);
Vector2 c = new Vector2(3, 0);
Vector3 res = b - a;//(1,2,0)
print(res);
print(res * 10);
print(res / 5);
print(a + b);
类Random
–计算机的随机数是伪随机数,实质为将按随机运算出来时的数值分布展示出来
一、静态方法
void Start () {
//2、InitState
Random.InitState(0);//相同的种子,生成随机数序列完全一致,可用于测试
//实际应用--保证每次生成序列不一致
Random.InitState((int)System.DateTime.Now.Ticks);//long类型System.DateTime.Now.Ticks--现在之间与时间戳之差(始终在增加)
}
void Update () {
//1、Range
print(Random.Range(4, 10));//注意:对于整数,max是不包含的,即取4,5……9
print(Random.Range(4, 5f));//注意:对于浮点数,max是包含的
if (Input.GetKeyDown(KeyCode.Space))
{
print(Random.Range(4, 100));//26,68,42,13……
print((int)System.DateTime.Now.Ticks);
}
}
二、静态变量–insideUnitCircle & insideUnitSphere
public Transform cube;
void Update () {
cube.position = transform.position = Random.insideUnitCircle * 5;//随机取二维单位圆内的位置
cube.position = transform.position = Random.insideUnitSphere * 5;//随机取三维单位球体内的位置
}