Unity3d游戏中常用API函数

野子电竞数据官网改版https://www.xxe.io/全新登场
1.Instantiate

原型:public static Object Instantiate(Object original, Vector3 position, Quaternion rotation);

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

注:

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

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

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

Game Object p = Instantiate ();

Rigidbody m = p.GetComponent ();

2.位移函数

(1)transform.Translate

原型:public void Translate(Vector3 translation, Space 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 point, Vector3 axis, float angle);

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

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

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

4.Physics.OverlapSphere

原型:

public static Collider[] OverlapSphere(Vector3 position, float radius, int layerMask = AllLayers, QueryTriggerInteractionqueryTriggerInteraction = QueryTriggerInteraction.UseGlobal);
作用:用于捕捉以position为圆心的一个圆内的所有碰撞器,返回值会碰撞器数组,一般用于OnCollisionEnter()触发碰撞检测时获取碰撞物(如手雷爆炸等)
例:Collider[] cols = Physics.OverlapSphere (transform.position,explosionRadius);
5.Rigidbody.AddExplosionForce
原型:public void AddForce(Vector3 force, ForceMode 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/数值)时间,便于进行游戏截图

    原文作者:待你幼稚完
    原文地址: https://segmentfault.com/a/1190000020274177
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞