Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by me2000 · Oct 16, 2015 at 06:59 PM · javascriptscripting problemaiscriptingbasicsfirst person shooter

OnTriggerEnter called only one time?!!

Im trying to do a simple shooting AI but the ai doesn't stop shooting when the Player leaves the collider, he just keeps shooting for ever, anyone could fix it please?

 var Bullet : Rigidbody;
 var Spawn : Transform;
 var BulletSpeed : float = 1000;
 var shootsound : AudioClip;
 var attack : int = 0;
 var FireRate = 1;
 var canfire = true;
 
 function OnTriggerEnter(pInSight : Collider){
     if(pInSight.gameObject.tag == "Player"){
         attack = 1;
     }
 
     if(!pInSight.gameObject.tag == "Player"){
         attack = 0;
     }
 }
 
 
 function Update () {
     if(attack == 1)
     {
         Shoot();
     }
 }
 
 function Shoot () {
     if(canfire == true){
         var bullet2 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
         bullet2.AddForce(transform.forward *BulletSpeed);
         GetComponent.<AudioSource>().PlayOneShot(shootsound);
         canfire = false;
         yield WaitForSeconds(FireRate);
         canfire = true;
     }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiegoSLTS · Oct 17, 2015 at 01:04 AM 0
Share

I think other people already answered, but just wanted to note that you might think this line does something different than it's actually doing (I guess it shouldn't even compile):

 if(!pInSight.gameObject.tag == "Player")

If you want to write "player tag is NOT 'Player'" the proper condition should be:

 if(pInSight.gameObject.tag != "Player") //note where the ! is placed

or:

 if (!(pInSight.gameObject.tag == "Player")) //note the extra parentheses

Just like "==" means "check if both sides are equal", the "!=" means "check if both sides are different". The not operand "!" before a variable is used with bool variables since it's the only place where "not the value of this variable" means something correctly defined ("not true" means "false", "not false" means "true"... you can't do that with ints, floats, strings, etc, etc).

5 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by Oribow · Oct 16, 2015 at 07:52 PM

OnTriggerEnter is only called when something enters the trigger. Use OnTriggerExit instead and always try searching before posting!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zephire · Oct 16, 2015 at 07:53 PM 1
Share

Edited. Correct and slightly faster typed by Oribow :)

avatar image me2000 · Oct 16, 2015 at 08:02 PM 0
Share

Thanks very helpful

avatar image
1

Answer by Matheuz · Oct 16, 2015 at 10:33 PM

The OnTriggerEnter is called when the player enters the Trigger. To register when something leaves the Trigger area, use OnTriggerExit instead.

Also, if Shoot is a coroutine, you can't simply call it, you need to call it using StartCoroutine():

http://docs.unity3d.com/ScriptReference/MonoBehaviour.StartCoroutine.html

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DiegoSLTS · Oct 17, 2015 at 12:57 AM 1
Share

UnityScript is less strict with coroutines, if the method has a yield in it then it runs as a coroutine, there's no need for the StartCoroutine method.

avatar image Matheuz · Oct 17, 2015 at 01:07 AM 0
Share

Oh, didn't know that. Thanks.

avatar image
1

Answer by omegacraft · Oct 16, 2015 at 10:33 PM

you could use the OnTriggerExit () function like this:

  var Bullet : Rigidbody;
  var Spawn : Transform;
  var BulletSpeed : float = 1000;
  var shootsound : AudioClip;
  var attack : int = 0;
  var FireRate = 1;
  var canfire = true;
  
  function OnTriggerEnter(pInSight : Collider){
          if(pInSight.gameObject.tag == "Player"){
          attack = 1;
      }

  function OnTriggerExit(pInSight : Collider){
          attack = 0;
      }
  }
  
  
      function Update () {
      if(attack == 1)
      {
          Shoot();
      }
 }
  
  function Shoot () {
      if(canfire == true){
          var bullet2 : Rigidbody = Instantiate(Bullet,Spawn.position,Spawn.rotation);
          bullet2.AddForce(transform.forward *BulletSpeed);
          GetComponent.<AudioSource>().PlayOneShot(shootsound);
          canfire = false;
          yield WaitForSeconds(FireRate);
          canfire = true;
          }
  }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Badgerfish · Oct 17, 2015 at 02:29 AM

If you want the player to be shot only while in the collider use: OnTriggerStay()

If you want the player to be shot only when entering the collider: OnTriggerEnter() However this will only happen once (specifically when the collider is entered, hence the name)

If you want the player to be shot only when exiting the collider: OnTriggerExit() However this will only happen once (specifically when the collider is exited, hence the name)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by meat5000 · Oct 16, 2015 at 11:58 PM

Its very simple.

  if(attack == 1)
  {
      Shoot();
      attack = 0;
  }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Java Script error Q's MissingReferenceExeption BCE0034 and BCE005 0 Answers

How would i make it transform player.position =target.position? 2 Answers

More Efficient way? 1 Answer

Its not playing the Audio? 2 Answers

identifying the index of a gameobject in an array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges