个人开发Unity3d游戏中常用API函数

    xiaoxiao2021-03-25  94

    1.Instantiate

    原型:public static Object Instantiate(Object originalVector3 positionQuaternion rotation);

    作用:生成对象实例。可用于新生成子弹/炮弹/物体,也可用于刷新敌人

    注:

    1)该函数返回值在monodevelop中查看返回值为GameObject,官方api手册中为Object

    2)该函数共有4个不同的版本对应不同的传参,以上为最常用版本

    3)获取新生成的物体的属性/组件时,需要定义变量储存以便访问,如:

    Game Object p = Instantiate ();

    Rigidbody m = p.GetComponent<Rigidbody> ();

    2.位移函数

    (1)transform.Translate

    原型:public void Translate(Vector3 translationSpace relativeTo = Space.Self);

    作用:使某物在三维世界中“瞬移”

    例:transform.Translate (new Vector3 (0, 0, 1) * moveSpeed * Time.deltaTime);

    注:参数为矢量,必须带方向,可加速度变量乘在参数内

    (2)velocity(对于刚体)

    原型:public Vector3 velocity;

    例:r.velocity = shootPoint.forward * shootPower;

    注:赋值刚体速度,同样也为矢量,适用物理定律

    3.transform.RotateAround

    原型:public void RotateAround(Vector3 pointVector3 axis, float angle);

    作用:使某物绕另一物体的哪一跟轴旋转

    例:transform.RotateAround (center.GetComponent<Transform>().position,Vector3.up,-70 * Time.deltaTime);

    使transform绕着center的y轴旋转,速度为70

    4.Physics.OverlapSphere

    原型:

    public static Collider[]  OverlapSphere( Vector3  position, float  radius, int  layerMask = AllLayers,  QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal); 作用:用于捕捉以position为圆心的一个圆内的所有碰撞器,返回值会碰撞器数组,一般用于 OnCollisionEnter()触发碰撞检测时获取碰撞物(如手雷爆炸等) 例:Collider[] cols = Physics.OverlapSphere (transform.position,explosionRadius); 5.Rigidbody.AddExplosionForce 原型:public void AddForce(Vector3 forceForceMode mode = ForceMode.Force); 作用:增加力场在某点上,可用于爆炸等场景模拟 例: rb.AddExplosionForce (explosionPower,transform.position,explosionRadius); 在rb上增加一个力量为explosionPower,位置为自己,爆炸半径为explosionRadius的力 6.Destory 原型: public static void  Destroy ( Object   obj , float  t  = 0.0F); 作用:在场景中删除某物 例:Destroy (gameObject); //删除gameObject   Destroy (se,delayClearExplosion); //在delayClearExplosion秒后删除se 待更新

    ·事件函数:

    事件函数总览:

    void Reset():当脚本被附加在GameObject上或者在面板中点击脚本的reset键时执行

    void FixedUpdate():以固定帧率调用,有可能会出现一帧被调用多次,用于与运动相关的代码控制

    ·Time类

    captureFramerate:将游戏速度减缓至(1/数值)时间,便于进行游戏截图

    Time.captureFramerate = 25;

    deltaTime:计算每帧之间的间隔事件,常用于时间累积,或者控制跟运动相关的代码时,在参数中相乘

    frameCount:计算到目前为止的总帧数(frameCount当所有Awake都执行完毕后开始计算,在Awake执行期间,值为未定义)

    timeScale:控制游戏运行速度,影响几乎所有Time类成员(主要影响deltaTime)可用于游戏暂停,默认值为1,数值大于1为加速,小于1为减速,为0时暂停且FixedUpdate()将不会执行

    转载请注明原文地址: https://ju.6miu.com/read-26059.html

    最新回复(0)