今天要做一个射击游戏,本来想着要让小球发射出去之后能够消失(因为小球过多之后会造成系统的卡顿,但是按照网上的操作之后发现destroy不但会将cube(clone)删除,同时会删除prefab预制体)
搜了好长时间无法解决,于是自己加了一行代码,顺利解决!附代码:
/ A code block
var foo = ‘bar’;
```javascript
// An highlighted block
var foo = 'bar';
1. using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class sheji : MonoBehaviour {
// Use this for initialization
void Start () {
}
int speed = 7;
public Transform newobject;
void Update () {
float x = Input.GetAxis("Horizontal") * Time.deltaTime*speed;
float z = Input.GetAxis("Vertical") * Time.deltaTime*speed;
transform.Translate(x, 0, z );
// print(x);
if(Input.GetButtonDown("Fire1"))
{
Transform n= Instantiate(newobject, transform.position, transform.rotation);
Vector3 fwd = transform.TransformDirection(Vector3.forward);
n.GetComponent<Rigidbody>().AddForce(fwd * 2000);
Destroy(n.gameObject, 5);*
}
}