您的位置:首页 > 移动开发 > Unity3D

unity之常用核心类习题总结

2014-12-19 20:07 127 查看
一、用数组存储3个cube,点击鼠标一次消失一个?(这个题有两种解答)

1、public class DestoryCube : MonoBehaviour {

public GameObject[] cube; //创建一个数组的cube

int i = 0; //数组的下标是从零开始的

float fireTime = 0.5f;//发射时间

float nextTime = 0.0f;//间隔时间

Update is called once per frame

void Update ()

{

if (Input.GetButton("Fire1") && Time.time>nextTime){

nextTime = fireTime + Time.time;

Destroy(cube[i]);

i++;

print(i);

}

2、if (Input.GetButtonDown("Fire1"))

{

Destroy(cube[i]);

i++;

}

二、用克隆方式发射球体子弹?

public class Fire : MonoBehaviour {

public GameObject bullet; //这是在游戏对象中创建一个bullet

public float fireTime = 0.5f;//发射时间

float nextTime = 0.0f;//间隔时间

void Update () {

if(Input.GetButton("Fire1") && Time.time>nextTime){

nextTime = fireTime + Time.time;

GameObject.Instantiate(bullet);

GameObject go= GameObject.Instantiate(bullet,new Vector3(0,3,0),Quaternion.identity) as

GameObject;

go.rigidbody.AddForce(0,0,1000);

}

if (Input.GetButtonDown("Fire1"))

{

GameObject.Instantiate(bullet);

}

这是我们今天讲的两个例题,欢迎来到我们的狗刨网来看新的更新内容。网址是:http://www.gopedu.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: