在此场景中,当用户按boxes
键space
与其他框碰撞然后得分增加时,有两个box1
。但是我面临的问题是,当box1
与其他方框碰撞然后box1
落在地面上时,它的位置值不会停止。第二是当分数增加时如何再次重新生成该盒子。
public class box1 : MonoBehaviour {
Rigidbody2D rigid;
void Start () {
rigid = GetComponent<Rigidbody2D> ();
rigid.isKinematic = true;
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space)) {
rigid.isKinematic = false;
}
}
}
专栏2:
public class boxer2 : MonoBehaviour {
public int timeshit;
public Text texter;
// Use this for initialization
void Start () {
timeshit = 0;
texter.text = "Score Fall : " + timeshit;
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D other){
timeshit++;
texter.text = "Score Fall : " + timeshit;
}
}
新代码已更新:
public class boxer2 : MonoBehaviour {
public GameObject boxObj;
public int timeshit;
public Text texter;
void Start () {
timeshit = 0;
texter.text = "Score Fall : " + timeshit;
}
void Update () {
}
void OnCollisionEnter2D(Collision2D col){
timeshit++;
texter.text = "Score Fall : " + timeshit;
if (col.gameObject.tag == "Player") {
Destroy(col.gameObject,0.5f);
}
Instantiate(boxObj, new Vector3(0.04f, 0f, 0f), Quaternion.identity);
}
}
答案 0 :(得分:0)
Declare a Public GameObject boxObj;//Don't forget to assign your box's prefab
void OnCollisionEnter2D(Collision2D other){
timeshit++;
texter.text = "Score Fall : " + timeshit;
Destroy(col.gameObject,0.1f);
// if (col.gameObject.tag == "Player") {//add a tag to your box and check it before destroying a gameObject
// Destroy(col.gameObject,0.1f);
// }
Instantiate(boxObj, new Vector3(x?F, y?, z?), Quaternion.identity);
// x,y,z are your desired positions
}
还要查看此链接 http://docs.unity3d.com/Manual/CreateDestroyObjects.html http://docs.unity3d.com/ScriptReference/Object.Instantiate.html
你需要从预制件中分配旅行箱对象的参考而不是从层次结构中分配,因为当层次结构中的对象被破坏时,它将首次工作,但第二次尝试无法创建对象。在你的情况下你应该遵循thre绿色箭头。