Unity学习笔记(3):一些常用API和应用场景

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

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

控制物体旋转相关:
Quaternion: 四元数

void Rotating(float h, float v)
{
    Vector3 targetDir = new Vector3(h, 0, v);
    Quaternion targetRotation = Quaternion.LookRotation(targetDir, Vector3.up);
    Rigidbody r = GetComponent<Rigidbody>();
    Quaternion newRotation = Quaternion.Lerp(r.rotation, targetRotation, turnSmoothing * Time.deltaTime);
    r.MoveRotation(newRotation);
}

h为水平输入,v为垂直输入,该代码实现角色根据水平方向的输入旋转的功能

猜你喜欢

转载自blog.csdn.net/haowenlai2008/article/details/82717956