C# void oncollisionenter发生了多少次?

C# void oncollisionenter发生了多少次?,c#,unity3d,collision-detection,collision,C#,Unity3d,Collision Detection,Collision,Unity 3d中有没有办法检测碰撞发生了多少次 例如,如果3次,则杀死敌人 或者如果是两次,那么寿命会减少50% 我想用void OnCollisionEnter函数来实现这一点 这是我的AI代码和我的玩家代码: public Transform[] Targets; private int DestPoint = 0; private NavMeshAgent Agent; public Transform Player; public Rigidbody Bullet; public Tr

Unity 3d中有没有办法检测碰撞发生了多少次

例如,如果3次,则杀死敌人

或者如果是两次,那么寿命会减少50%

我想用
void OnCollisionEnter
函数来实现这一点

这是我的AI代码和我的玩家代码:

public Transform[] Targets;
private int DestPoint = 0;
private NavMeshAgent Agent;
public Transform Player;
public Rigidbody Bullet;
public Transform Instantiator;
public float BulletSpeed;
public float fireRate;
private float nextFire = 0F;

void Start()
{
    Agent = GetComponent<NavMeshAgent> ();
    Agent.autoBraking = false;
}

void Update()
{
    if (Vector3.Distance(transform.position, Player.position) < 100)
    {
        transform.LookAt (Player);
        if (Time.time > nextFire)
        {
            nextFire = Time.time + fireRate;
            Agent.Stop ();
            Shoot ();
        }
    }
    else if (Vector3.Distance(transform.position, Player.position) > 100)
    {
        GotoNextPoint ();
    }
}

void GotoNextPoint()
{
    Agent.destination = Targets [DestPoint].position;
    DestPoint = (DestPoint + 1) % Targets.Length;
}

void Shoot()
{
    Rigidbody Clone = Instantiate (Bullet, Instantiator.position, Instantiator.rotation) as Rigidbody;
    Clone.AddForce (Instantiator.forward * Time.deltaTime * BulletSpeed);
}

public float Speed;
public float RotationSpeed;
public Rigidbody Bullet;
public float BulletSpeed;
public Transform Instantiator;
public float fireRate;
private float nextFire = 0F;

void Update()
{
    if (CrossPlatformInputManager.GetAxis("Vertical") > 0)
    {
        transform.Translate (Vector3.forward * Time.deltaTime * Speed);
    }
    if (CrossPlatformInputManager.GetAxis("Vertical") < 0)
    {
        transform.Translate (Vector3.back * Time.deltaTime * Speed);
    }
    if (CrossPlatformInputManager.GetAxis("Horizontal") > 0)
    {
        transform.Rotate (Vector3.up * Time.deltaTime * RotationSpeed);
    }
    if (CrossPlatformInputManager.GetAxis("Horizontal") < 0)
    {
        transform.Rotate (Vector3.down * Time.deltaTime * RotationSpeed);
    }

    if (CrossPlatformInputManager.GetButtonDown ("Shoot") && Time.time > nextFire)
    {
        nextFire = Time.time + fireRate;
        Rigidbody Clone = Instantiate (Bullet, Instantiator.position, Instantiator.rotation);
        Clone.AddForce (Instantiator.forward * Time.deltaTime * BulletSpeed);
    }
}
public Transform[]目标;
私有点=0;
私人代理;
公共转换播放器;
公共刚体子弹;
公共转换实例化器;
公共交通速度;
公共浮式灭火器;
私有浮点nextFire=0F;
void Start()
{
Agent=GetComponent();
Agent.autoBraking=false;
}
无效更新()
{
if(矢量3.距离(变换位置、玩家位置)<100)
{
变换。注视(运动员);
如果(Time.Time>nextFire)
{
nextFire=时间。时间+燃速;
Agent.Stop();
射击();
}
}
else if(矢量3.距离(变换位置、玩家位置)>100)
{
GotoNextPoint();
}
}
void GotoNextPoint()
{
Agent.destination=目标[DestPoint]。位置;
DestPoint=(DestPoint+1)%Targets.Length;
}
空射()
{
刚体克隆=实例化(Bullet,Instantiator.position,Instantiator.rotation)为刚体;
Clone.AddForce(instantior.forward*Time.deltaTime*BulletSpeed);
}
公众浮标速度;
公众浮标轮换速度;
公共刚体子弹;
公共交通速度;
公共转换实例化器;
公共浮式灭火器;
私有浮点nextFire=0F;
无效更新()
{
if(CrossPlatformInputManager.GetAxis(“垂直”)>0)
{
transform.Translate(Vector3.forward*Time.deltaTime*Speed);
}
if(CrossPlatformInputManager.GetAxis(“垂直”)<0)
{
transform.Translate(Vector3.back*Time.deltaTime*Speed);
}
if(CrossPlatformInputManager.GetAxis(“水平”)>0)
{
transform.Rotate(Vector3.up*Time.deltaTime*RotationSpeed);
}
if(CrossPlatformInputManager.GetAxis(“水平”)<0)
{
transform.Rotate(Vector3.down*Time.deltaTime*RotationSpeed);
}
if(CrossPlatformInputManager.GetButtonDown(“拍摄”)和&Time.Time>nextFire)
{
nextFire=时间。时间+燃速;
刚体克隆=实例化(Bullet,Instantiator.position,Instantiator.rotation);
Clone.AddForce(instantior.forward*Time.deltaTime*BulletSpeed);
}
}

OnCollisionCenter函数没有用于此操作的内置变量或函数。您必须为此创建一个变量,然后在每次发生碰撞时递增该变量。没有其他方法可以做到这一点

int collisonCounter = 0;

void OnCollisionEnter(Collision collision)
{
    //Increment Collison
    collisonCounter++;

    if (collisonCounter == 2)
    {
        //reduce the life by 50 percent

    }

    if (collisonCounter == 3)
    {
        // kill the enemy


        //Reset counter
        collisonCounter = 0;
    }
}

除非你只有一个敌人,而且唯一与玩家发生碰撞的是敌人,否则你会希望在敌人的物体上有这个。否则,我可能会两次与一个敌人相撞,然后一次与另一个敌人相撞,然后一枪打死它。@code11当然可以,但你可以将它放在玩家身上,然后检查它相撞的游戏对象的
标签。如果您不必将脚本附加到每个敌人身上,这将为您节省一些内存空间。同意,但您将需要一个计数器(可能在某种字典中)用于您遇到和碰撞的每个独特敌人。我明白您现在所说的。它应该附属于敌人。