这几天经过Unity官网的Unity Scripting Tutorials的视频学习Unity脚本,观看的过程当中作了记录。如今,整理了一下笔记,供本身之后和其余初学者参考。编辑器
使用这两个方法控制物体运动。
Translate对应position:ide
transform.Translate(Vector3); // Amount in each axis to move by
Rotate对应rotation:学习
transform.Rotate(Vector3, // Axis around which to rotate float) // amount to rotate by
transform.LookAt(target);
用于移动物体Z轴使之对准目标物体。
将其绑定到相机上能够实现运动物体的跟踪。3d
用于平滑某种转变。code
空间移动:orm
Vector3.Lerp(Vector3 from, // 起点。一般应设为当前坐标 Vector3 to, // 终点 float t); // 0~1之间的值。值越大,返回值越接近终点,移动也越快。
数值变换:视频
Mathf.Lerp(float from, float to, float t);
颜色渐变:对象
Color.Lerp(Color from, Color to, float t);
GetButton和GetKey的区别:ip
GetButton/Up/Down的区别:字符串
GetAxis(axisName)经过获取某个轴的值来了解用户的输入。
鼠标在Collider或GUI组件上按下时调用该方法。
同类方法:
建立prefab的副本,返回对象的引用。
使用时先用代码建立GameObject的变量,而后回到编辑器界面将prefab拖入变量中。
调用名叫method的方法:
Invoke(method, delay);
每隔time重复调用method方法:
InvokeRepeating(method, delay, time);
取消Invoke:
CancelInvoke();
取消名叫method的Invoke:
CancelInvoke(method);
利用了C#的yield
,来实如今屡次Update中执行一个行为。
开始一个Coroutine:
StartCoroutine(IEnumerator routine);
或者接受一个string参数表示方法的返回IEnumerator的方法的名字,后面跟着参数。
例:
StartCoroutine(Func(param)); StartCoroutine("Func", param);
中止一个Coroutine,用StopCoroutine。一样有两种形式。
用来表示旋转的类。讲了三个内容:
LookRotation(Vector3 forward);
返回使物体转向forward方向的Quaternion。
Slerp(Quaternion from, Quaternion to, float t);
旋转角度。相比Lerp,在中间时快,两头时慢。
Quaternion.identity
是一个静态变量,表示没有旋转。