制作水果忍者-JS-2

来源:互联网 发布:侠盗飞车作弊软件 编辑:程序博客网 时间:2024/04/30 10:17

之前已经了解过了instantiate这个函数了,今天产生水果运用了这个函数。其实并不难,知道函数的结构,参数。运用它就行了!!

先是将水果的模型导入,然后将水果变出预制物体,然后编写JS代码,定义数组,然后在Unity中把那三个模型放入,然后测试摄像机在背景的X的距离,为了让产生的水果能在摄像机的范围内,用同样的方法也测试了Z的合适距离。(方法就是用水果的模型去放在各个位置,然后知道它的坐标就能测试去范围来)

然后就是写一种方法去产生水果,就是利用instantiate去写函数。然后制作一个矢量力去让产生的水果拥有一个初速度,在赋予上去就行了。接着为让产生的水果更具有真实性,就产生了一个扭矩力,让其旋转。

#pragma strictvar fruitmodel:GameObject[];//放置水果模型的var boom:GameObject;//炸弹模型var diretion:Vector3;var powerscale:float=0.3;//给水果一个大小力度function Start () { InvokeRepeating("Createfruit",2,0.3);}function Update () {}function Createfruit(){var IsBoom:boolean=false;var x:float = Random.Range(-3.1,3.1);//测试出水果产生出在摄像机的X轴距离var z:float = Random.Range(0.5,1.5);//同上Y轴距离if(!IsBoom)var ins:GameObject = Instantiate(fruitmodel[Random.Range(0,fruitmodel.length)],transform.position + Vector3(x,0,z),Random.rotation);//产生水果函数Instantiateelseins = Instantiate(boom,transform.position + Vector3(x,0,z),Random.rotation);var power:float = Random.Range(1.5,1.8) * (-Physics.gravity.y) * powerscale;diretion = Vector3(-x * 0.05 * Random.Range(0.1,0.5),1,0);//定义一个适量力,使其有方向有大小!!diretion.z=0;ins.GetComponent(Rigidbody).velocity = diretion * power;//产生一个初速度ins.GetComponent(Rigidbody).AddTorque(Random.onUnitSphere * 0.1,ForceMode.Impulse);//产生一个扭矩力,使水果开始旋转}
最后利用InvokeRepeating去多次调用函数就行产生多个水果了。


1 0
原创粉丝点击