如何同时获得相同克隆的多个碰撞接触? Unity2D / Unity3D

时间:2014-05-15 03:35:52

标签: c# unity3d

当相同克隆发生2次不同的碰撞时,我希望在2个不同的位置实例化2件事。

但是当它们在同一时间发生时,只有1次碰撞被调用。

此代码实例化了对其碰撞位置的触发,但是当同一个克隆的2个不同碰撞同时发生在2个不同的位置时,实例化器只会实例化其中一个碰撞的位置,但不会其他碰撞。

顺便提一下,这是2D。

让我们说有4枚火箭(它们都有相同的脚本和行为,因为它们是克隆)。第1个与第2个碰撞,第3个碰撞第4个与第1个和第2个相同的时间碰撞。这应该创建2个碰撞点。但它只是在其中一个碰撞点上实例化。

using UnityEngine;
using System.Collections;

public class Rocket : MonoBehaviour {

//Variables
bool exploded;
bool explosion;
Vector3 rocketPoint;

//The tag of this object is "rocket"

void Start ()

{
exploded = true;
explosion = false;
}

void Update ()

{

if(explosion && exploded)

{
InvokeRepeating("fire",0,60);
exploded = false;
explosion = false;
}

}

void OnCollisionEnter2D(Collision2D col)

{
if (col.gameObject.CompareTag ("rocket"))
{
//Gets the position of the collision with the other rockets.
ContactPoint2D contact = col.contacts[0];
rocketPoint = contact.point;

explosion = true;

//Calls function that destroys the rocket after 0.01 seconds after colliding
InvokeRepeating("destroyThis",0.01f,0.01f);
}
}

//The "Instantiator"
void fire()

{
Instantiate(Resources.Load("Fire"), rocketPoint, Quaternion.identity);
exploded = true;
}

//Destroys the rocket after 0.01 seconds after colliding.
void destroyThis()

{
Destroy (gameObject);
}

}

0 个答案:

没有答案