个人学习第三章节: 常见脚本
1.Awake( )与 Start()
Awake( )在游戏创建时调用, 用于设置游戏初始化的参数
Start( )在脚本被调用, 执行在所有 Update( )之前, 用于赋值变量
2.Update( )与 FixedUpdate()
Update( )用于非刚体对象的运动, 诸如定时器, 检测输入等, Update 受制于机器性能帧率
FixedUpdate()用于刚体对象运动, 不受制于机器性能, 有固定调用间隔.
3.Vector3.Dot( )与 Vector3.Cross( )
Vector3.Dot( )用于表示两个向量的乘积, 乘积为 - 1 则为反方向, 为 0 则两个向量垂直
Vector3.Cross( )用于计算两个向量交叉乘积, 乘积结果为垂直于两个向量的新的向量
4.transform.Translate( )与 transform.Rotate( )
transform.Translate( )用于指定向量的位移
transform.Rotate( )用于选定向量为旋转中心 (视线为向量方向) 的逆时针旋转
这里 z 轴正向为 transform.forward 负向为 transform.back
y 轴正向为 transform.up 负向为 transform.down
x 轴正向为 transform.left 负向为 transform.right
5.transform.LookAt(target)
transform.LookAt(target)用于指定物体 z 轴始终指向 target, 用于 camera 类似于第一人称视角
6.Math.Lerp( )
举例: float result = Mathf.Lerp (3f, 5f, 0.5f); 得到结果为 4
- Vector3 from = new Vector3 (1f, 2f, 3f);
- Vector3 to = new Vector3 (5f, 6f, 7f);
Vector3 result = Vector3.Lerp (from, to, 0.75f); 得到结果为(4,5,6)
补充 SmoothDamp( currentPosition ,targetPosition ,ref Velocity ,smoothTime )
currentPosition: 我们正在的位置
targetPosition: 目标移动位置
ref Velocity: 不用管, Vector3 Velocity = Vector3.zero;
smoothTime: 移动所要时间 注意这里 smoothTime = 3f 并不为 3s(感觉大概是)
用于随着时间的推移, 逐渐将向量改变为所需的目标.
7.Destory( )
用于 gameobject 或者 compoent 的清除
Destory(target,3f) 目标将在 3s 后摧毁
8.Input.GetButton( )与 Input.GetKey( )
当按下按键, GetButtonDown( )仅按下触发一次为 True, 其他为 False
GetButtonUp( )仅谈起触发一次为 True, 其他为 False
GetButton( )只要按键按下未弹起, 即为 True
GetKey( ) GetKeyDown( ) GetKeyUp( )用法一样
Input.GetKey( )设定按键在 Unity 窗口 Edit/Project Settings/Input 中
9.GetAxis( ) http://www.yaoqikj.com/
GetAxis( )类似于 GetKey( ), 设定按键在 Unity 窗口 Edit/Project Settings/Input 中
主要用法 GetAxis("Vertical"),GetAxis("Horizontal" http://www.yaoqikj.com/ )
按下方向键正向, GetAxis( )数值从 0 到 1 增加, 然后松开返回值为 0
在 Input 设置中修改 Gravity 大小, 决定了换向时候变化的速度, 数值越大速度越快
修改 Sensitivity 大小, 决定了按键从数值变化的快慢, 数值越大速度越快
GetAxisRaw( )该值的范围为 - 1 ... 1, 当 GetAxis 处理物体移动有明显跳帧问题, 则使用此方法平滑移动
10.OnMouseDown( )
OnMouseDown( )用于鼠标点击事件
其他鼠标事件还有
OnMouseEnter( )当鼠标移动到对象上触发
OnMouseOver( )当鼠标停在对象上触发
OnMouseExit( )当鼠标移动到对象后离开触发
OnMouseDrag( )当鼠标到对象上按住触发
11.GetComponent<>( )
可以用这个方法获取其他游戏部件, 或者是其他的脚本.
当获取其他组件元素时候, 建议利用方法获取组件, 然后 target.getCompent<BoxCollider>( )
还有一种获取游戏部件方式
- 13.Instantiate( )
- Instantiate(gameObject,target.transform.position,target.transform.rotation)
- 15.InvokeReapting( )
- InvokeReapting("函数名称",time,durTime)
- 17.delegate void Function();
- Function() function;
来源: http://www.bubuko.com/infodetail-2875344.html