unity学习笔记3 一些常用api和应用场景

Mathf.Lerp(float a,float b,float t)插值函数,当a < b时往a中插入t,以此来实现颜色,声音等渐变效果。

GameObject.FindWithTag(string tag)通过标签得到实例化的对象,实现跨脚本操作对象

控制物体旋转相关:
Quaternion: 四元数
void Rotating(float h, float v)
{
Vector3 targetDir = new Vector3(h, 0, v);
Quaternion targetRotation = Quaternion.LookRotation(targetDir, Vector3.up);
Rigidbody r = GetComponent();
Quaternion newRotation = Quaternion.Lerp(r.rotation, targetRotation, turnSmoothing * Time.deltaTime);
r.MoveRotation(newRotation);
}
h为水平输入,v为垂直输入,该代码实现角色根据水平方向的输入旋转的功能