user7387184
user7387184

Reputation:

how many times void oncollisionenter happens?

Is there a way in Unity 3d to detect how many times a collision happened ?

For example, if 3 times then kill the enemy.

or if two times then reduce the life by 50 percent.

I want to do this with the void OnCollisionEnter function..

This is my AI code and my Player Code :

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);
    }
}

Upvotes: 3

Views: 189

Answers (1)

Programmer
Programmer

Reputation: 125275

The OnCollisionEnter function does not have built in variables or functions for this. You have to create a variable for this then increment the variable each time there is a collison. There is no other way to do this.

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;
    }
}

Upvotes: 1

Related Questions