关于Unity的学习三--Vector向量

1.Vector向量的使用;
例子:
Vector3 v1 = new Vector3 (3, 4, 5);
//Vector v = new Vector(); //创建Vector的两种方式
//v1.x = 3; v1.y = 4; v1.z = 5;
print (v1);
Vector3 v2 = new Vector3 (1, 2, 3);
Vector3 v3 = v1 + v2;
Vector3 v4 = v1 - v2;
//点乘,用dot,判断角度的
float dot = Vector3.Dot (v1, v2);
print (dot);

	print (Vector3.up);
	print (Vector3.forward);
	print (v1.magnitude);// 模
	print (v1.sqrMagnitude);//模的平方
	print (v1.normalized);//单维化,用x、y、z分别除以模
	//计算法向量
	Vector3 a = new Vector3 (0, 0, 0);
	Vector3 b = new Vector3 (3, 0, 3);
	Vector3 c = new Vector3 (4, 0, 3);
	Vector3 side1 = b - a;
	Vector3 side2 = c - a;
	Vector3 normal = Vector3.Cross (side1, side2);
	normal.Normalize ();
	Debug.Log (normal);

2.欧拉角和四元数
例子:
print (this.transform.eulerAngles);
//欧拉角+转的角度 = 四元数(Quaternion)
this.transform.rotation = Quaternion.Euler (this.transform.eulerAngles
+ new Vector3 (0, -30, 0));
print (this.transform.eulerAngles);
3.MonoBehavior类
常见成员方法
GetComponent 返回游戏对象上指定名称的组件;
GetComponentsInChildren 返回游戏对象及其子对象上指定类型的所有组件
VectorTest[] array = GetComponentsInChildren ();
print (array.Length);
GetComponentInChildren 返回游戏对象及其子对象上指定类型的第一个找到的组件
GetComponents 返回游戏对象上指定名称的全部组件
SendMessage 挂载游戏对象的每一个脚本中调用指定名称的方法
Instantiate 动态的实例化一个对象
Destroy 销毁一个游戏对象、组件、或资源
DestroyImmediate 立即销毁物体

4.Lerp函数:a+(b-a)t
Quaternion q1 = Quaternion.Euler (new Vector3 (0, 0, 0));
Quaternion q2 = Quaternion.Euler (new Vector3 (0, 200, 0));
Quaternion des = Quaternion.Lerp (q1, q2, Time.time);
//Quaternion des = Quaternion.Lerp (q1, q2, Time.deltaTime);//会形成抖动 //this.transform.rotation = des;

	float f = Mathf.Lerp (1

, 20, 0.5f);
print (f);

	float angles = Mathf.LerpAngle (100, 700, 1.0f);
	print (angles);

void FixedUpdate(){

	Vector3 a = new Vector3 (0, 0, 0);
	Vector3 b = new Vector3 (3, 0, 10);
	Vector3 des = Vector3.Lerp (a, b, 0.5f);
	Vector3 des1 = Vector3.Lerp (a, b, Time.fixedTime);
	print (des1);
	
}

5.Input
Input是Unity中单独的一个类,可以处理键盘的敲击以及鼠标的点击事件
(1)获取鼠标的点击
例子:
void Update () {
if (Input.GetButton (“Fire3”)) {
print(“click”);
print(Input.mousePosition);
}
if (Input.GetButtonDown (“Fire3”)) {
print(“down”);
}
if (Input.GetButtonUp (“Fire3”)) {
print(“up”);
}

其中Fire1表示左键,Fire2表示右键,Fire3表示鼠标滚轮;
Input.mousePosition是一个三维的坐标,用于获取当前鼠标的像素坐标;

(2)用于监听键盘上的按键的状态
if (Input.GetKey (KeyCode.A)) {
print(“A pressed”);
}
if (Input.GetKeyDown (KeyCode.A)) {
print(“A down”);
}
if (Input.GetKeyUp (KeyCode.A)) {
print (“A up”);
}
其中GetKey是指定按钮时返回true,GetkeyDown是指定按钮的一帧时返回true,GetKeyUp是按下指定按钮然后抬起的一帧时返回true;
(3)
if (Input.GetMouseButton (0)) {
print(“0”);
}

	if (Input.GetMouseButton (1)) {
		print("1");
	}

GetMouseButton是指定鼠标按键按下时返回true,GetMouseButtonDown是指定按钮的一帧时返回true,GetMouseButtonUp是按下指定按钮然后抬起的一帧时返回true;

(4)anyKey变量:用来监听当前是否有按键按下,不管什么按键都会触发,若按住键不放则一直触发;
anyKeyDown:监听当前是否有按键按下,若按键不放只会在按住的时候触发一次,之后不会在触发,直到松开再次按下便能触发;
if (Input.anyKey) {
print(“any”);
}
(5)inputString:返回键盘在这一帧中输入的字符串,在返回的字符串中只包含有ASCII的字符串
public string str;
if (Input.inputString != “”) {
//str += Input.inputString;
print(Input.inputString);
}
(6)GetAxis方法和GetAxisRaw方法
两者都是获取虚拟轴的方法
print(Input.GetAxis("Horizontal”));//范围是 -1~1
print (Input.GetAxis ("Vertical”));//范围是 -1~1
如果GetAxis换成GetAxisRaw,则只有-1、0、1;

猜你喜欢

转载自blog.csdn.net/qq_36725286/article/details/89961042