1
\$\begingroup\$

There is are white and red two teams of tank which are auto spawning in certain time interval. both red and white type of tanks have triggers so when red and white tank come close enough, OntriggerEnter() is activated and both types of tank stop moving and start to fire each other. their movement is controlled by navemesh and both type of tanks are navmeshagent. Now What i want is when these two start firing each other both tank's trigger need to ignore collision of trigger with third tank until one of those two tanks die. Below is my code any help is appreciated thanks.

      public float RBdist;
      public GameObject Rbullet;
      public Transform targetofred;
      public float attackspeedofRtank;
 private void Start()
{
   // rwalktohit = GetComponent<Animator>();
     red = GetComponent<NavMeshAgent>();
    //cube2.velocity = 2f * (cube2.velocity.normalized);
    red.destination = targetofred.position;
//targetofred is the position of house of white tank or base of white tank.

    Rcollider = GetComponent<Collider>();     
}
      private void OnTriggerEnter(Collider whitettank)
      {

          if (whitettank.gameObject.tag == "whitetank")//tag of white
// tanks is "whitetank".

        {
          red.isStopped = true;
          twhitetank = whitettank.transform;
          transform.LookAt(twhitetank.position);           
          InvokeRepeating("Rfire", 1f, attackspeedofRtank);
         }

      }

private void Rfire()
{

     GameObject Rbulletclone = Instantiate(Rbullet, transform.position , 
    transform.rotation);
     Rbulletclone.transform.position = 
   Vector3.MoveTowards(transform.position , twhitetank.position, RBdist); 
}




private void OnTriggerExit(Collider whitettank)
{
    if (whitettank.gameObject.tag == "whitetank")
    {
        CancelInvoke("Rfire");
        red.isStopped = false;
    }

} 
\$\endgroup\$
  • \$\begingroup\$ Use whitettank.CompareTag("whitetank") instead of whitettank.gameObject.tag == "whitetank". It's more efficient. \$\endgroup\$ – Ed Marty Nov 28 '17 at 16:32
1
\$\begingroup\$

I think it should be as easy as:

if (whitettank.gameObject.tag == "whitetank" && !red.isStopped)
{
    // do stuff
}

This way the script will skip over your shooting-code after the first time. When you want to re-enable it just set red.isStopped to false again.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ to elaborate on this, essentially you're not disabling the trigger, just ignoring that something else has entered the area when the tank is already fighting. \$\endgroup\$ – Stephan Nov 27 '17 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.