之前不太系统,补充一下,记录一下学习到的内容技巧
这篇文章记录用到的实习功能的代码段
1.涉及刚体,放到FixedUpdate()方法而不是update()
同理,有Time.FixedUpdate,按照固定的物理时间被系统回调执行,与游戏帧率无关
2.访问其他游戏对象的方式
1>利用(与本物体之间的)关系访问
transform.Find("Plane2").Translate(0, 0, 1);
transform.parent.Translate(0, 0, 1);
2>利用名字/标签
GameObject tempgameobject = GameObject.Find("name");
GameObject temp2gameobject = GameObject.FindWithTag("Player");
3.利用事件回调方法
例如
private void OnTriggerEnter(Collider other)
{
other.GetComponent<Rigidbody>().AddForce(0, 0, 2);
}
4.利用组件,反过来推
3.向量
vector3.zero/one/forward/up/right
±*/ == !=
vector.Lerp/Slerp/OrthoNormalize/MoveTowards/SmoothDamp等方法
4.实例化游戏对象
private void OnTriggerEnter(Collider other)
{
other.GetComponent<Rigidbody>().AddForce(0, 0, 2);
Destroy(other.GetComponent<GameObject>());
GameObject newobject = Instantiate(name2, transform.position);
}
5.协同程序&中断
可以用yield 中断程序
也可以用WaitForSeconds类的实例化对象让协同程序休眠
void Start()
{
StartCoroutine(DoThing());
}
IEnumerator DoThing()
{
Debug.Log("协同");
// yield return null;
while (true)
{
yield return new WaitForSeconds(2);
Debug.Log("等了2s");
}
}
可以将多个协同程序连接,做一个简单测试
static int count = 0;
// Start is called before the first frame update
void Start()
{
StartCoroutine(DoThing1());
}
IEnumerator DoThing1()
{
Debug.Log(count+ "协同1");
count++;
yield return StartCoroutine(DoThing2());
count++;
Debug.Log(count + "结束协同1");
}
IEnumerator DoThing2()
{
count++;
Debug.Log(count + "协同2");
yield return new WaitForSeconds(2);
count++;
Debug.Log(count + "结束协同2");
}
在start方法里开启协同程序1,协同程序1中开启并等待执行doThing2程序,协同2 休眠2s后打印出结束协同2,该程序执行完,返回协同程序1,打印结束1,程序结束
5.常见类
1>Monobehaviour 每个脚本的基类
2>Transform类 存储&操作物体的位置、旋转、缩放/
还提供了父子关系/坐标系转化的函数
注明:right ->x轴 up ->y轴 forward ->z轴
3>rigidbody类
提供模拟物体在物理效果下的状态(受力/扭矩/也同样有position/rotation成员变量)
4>CharacterController类
角色控制器,常用于第一人称?第三人称的游戏角色控制,根据碰撞检测是否能够移动/不必添加刚体&碰撞器,且角色控制器不会受到力的影响??