Unity 3d 常用脚本
Collected by miccall (内容均来自于网络)
- Time.deltaTime :
void Update () {
transform.Translate(new Vector3(0,0,1) * Time.deltaTime);
// * Time.deltaTime 以后, 物体一秒运动一次
}
- Vector3(0,0,1) 相当于 Vector3.forward
transform.Translate() 和 transform.Rotate() 都是根据自身坐标 - LookAt() 可以让对象的正前方 ,朝向设定的坐标方向 。一般用于摄像机跟踪对象
- 平滑过渡 : SmoothDamp函数
- 插值 : Mathf.Lerp(起始值,结束值,比例) 同样的 Vector3 和Color 也有该方法 。
Instantiate(); //用来实例化对象 。。。 返回值是Object 。
Instantiate(Prefab);
//默认(0,0,0)
//以下是指定在某个对象的位置和角度创建 。。
Instantiate(Prefab,Transform.position , Transform.rotation);
- Rigidbody 添加力 AddForce() ;
AddForce(Transform.position * 倍数 ,ForceMode mode = ForceMode.Force) ;
AddForce(Transform.position * 倍数 ) ;
- 对象的销毁
Destory(Object gameObject , Time t );
- Invoke(); 延时调用某函数 。
InvokeRepeating(); 重复调用某函数
CanceInvoke(); 结束InvokeRepeating()的调用 。
Invoke("FunctionName",DelayTime s );
InvokeRepeating("FunctionName",FirstDelayTime s1 , CirclyDelayTime s2);
CanceInvoke("FunctionName");
四元数 。。Quaternion ,默认是(0,0,0,0)用Quaternion.identity 表示。
Quaternion.identity就是指Quaternion(0,0,0,0),就是每旋转前的初始角度,
是一个确切的值,而transform.rotation是指本物体的角度,值是不确定的,
比如可以这么设置transform.rotation = Quaternion.identity;
一个是值类型,一个是属性变量枚举类型 。。 Enumerations == Enums
enum Name{
N,E,S,W
};
//默认 : N==0,E==1,S==2,W==3
// enum Name : int{};
//声明 :
Name name ;
name.N;
GetComponentsInChildren<组件>();
这种方式是 大家经常用到的,这种是获取自己和它的孩子有同样的组件(比如 Rigidbody),返回一个数组,数组包括自己和它的孩子。如果他的孩子里面有的是不活跃SetActive(false);则获取不到。
所以就会有第二个参数GetComponentsInChildren<组件>(true);Transform组件
Variables
position Vector3类型,物体位置,相对于世界坐标系的值。就是矩阵的最后一行的值。
localPosition Vector3类型,物体相对于父元素的位置.
eulerAngles Vector3类型,旋转后的欧拉角相对世界坐标的值。简单理解为各向量坐标所转动的值就好了。
localEulerAngles Vector3类型,相对父元素的欧拉角。
right Vector3类型,表示x轴(U3里红色的轴)方向的单位向量。
up Vector3类型,表示y轴(U3里绿色的轴)方向的单位向量。
forward Vector3类型,表示z轴(U3里蓝色的轴)方向的单位向量。
rotation Quaternion类型(quatermion就是个四维数,比vector3多一个w变量表示旋转角度的),世界坐标旋转。.
localRotation Quaternion类型,本地坐标的旋转度数(相对父元素)。
localScale Vector3类型,相对本地坐标系缩放(就是矩阵的乘法)
parent Transform类型,他的父元素。
worldToLocalMatrix Matrix4*4类型,把一个点从世界坐标系的位置转换为本地坐标系位置。(终究得用矩阵,哈哈)只读
localToWorldMatrix Matrix4*4类型,把一个点从本地坐标系转换为世界坐标系。(只读)
root Transform类型,返回最上层的那个Transform,就是他父亲的父亲的父亲的。。。(如果有的话)
childCount int型,你子元素的数量
lossyScale Vector3类型,相对世界坐标系缩放这个功能很实用,传入的两个参数分别代表前方盯着的方向以及自己的上方向。它可以让一个GameObject转动脑袋盯着另一个物体。
public Transform target; void Update() { Vector3 relativePos = target.position - transform.position; Quaternion rotation = Quaternion.LookRotation(relativePos); transform.rotation = rotation; }