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

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

待更新

猜你喜欢

转载自blog.csdn.net/amcp9/article/details/61222445
今日推荐