Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ssiroo · Jul 29, 2014 at 03:25 PM · javascriptraycastai

Functions being ignored

Hello, I made this script but for some reason some functions are being ignored or something. This is the script :

     #pragma strict
     
     public var rocketPrefab : Transform;
     public var barrelEnd : Transform;
     var canShoot : boolean = true;
     var shootRate : float;
     var gun : Transform;
     var soundEffect : AudioClip;
     var player : Transform;
     var enemy : Transform;
     var minShoot : float = 2;
     var maxShoot : float = 4;
     var rayLength = 1000;
 
 function Start ()
 {
 //    e();
 }
 
 function Update ()
 {
     transform.LookAt(player);
     var hit : RaycastHit;
     var forward = transform.TransformDirection(Vector3.forward);
     if(Physics.Raycast(transform.position, forward, hit, rayLength))
     {
         if(hit.collider.gameObject.tag != "Player")
         {
             canShoot = false;
         }
         
         if(hit.collider.gameObject.tag == "Player")
         {
             canShoot = true;
         }
     }
     if (canShoot == true)
     {
         Shoot();
     }
 }
 
 function e()
 {
     canShoot=false;
     yield WaitForSeconds(Random.Range(1, 2));
     canShoot=true;
 }
 
 function w()
 {
     canShoot=false;
     yield WaitForSeconds(Random.Range(minShoot,maxShoot));
     canShoot=true;
 }
 
 function Shoot()
 {
         var rocketInstance : Transform;
         rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
         audio.PlayOneShot(soundEffect);
         w();
 }


The problem here is that the ai ignores(I think) the function "w". Some help would be really appreciated. Thank you for your time.

Comment
Add comment · Show 8
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 gjf · Jul 29, 2014 at 03:30 PM 0
Share

do you have a gameObject tagged with "Player"?

does the Shoot() function get called?

btw, ins$$anonymous$$d of this:

 if(hit.collider.gameObject.tag != "Player")
 {
     canShoot = false;
 }
 
 if(hit.collider.gameObject.tag == "Player")
 {
     canShoot = true;
 }

you can do this:

 canShoot = (hit.collider.gameObject.tag == "Player");



EDIT: do you get any error messages?

avatar image Ssiroo · Jul 29, 2014 at 03:35 PM 0
Share

Yes,I do have a gameObject tagged with "Player" and Shoot() does get called.

Also,thanks for the suggestion,it will save me a lot of time in the future.

Edit :

No,I don't get any error messages and I just noticed that the "w" function is being called but for some reason it ignores the yield WaitForSeconds part.

avatar image gjf · Jul 29, 2014 at 03:39 PM 0
Share

if Shoot() gets called, then w() should be too... since are there no further conditions to be met

what makes you think that "some functions are being ignored or something"?

avatar image Ssiroo · Jul 29, 2014 at 03:41 PM 0
Share

I don't know if 'ignored' is the right word for it but the ai is supposed to fire once per 2-4 seconds and not 100 bullets per second.

avatar image gjf · Jul 29, 2014 at 03:52 PM 0
Share

that's because you're resetting the canShoot variable every frame when the hit occurs.

try something like this:

 if(Physics.Raycast(transform.position, forward, hit, rayLength))
 {
     if((hit.collider.gameObject.tag == "Player") && canShoot)
     {
         Shoot();
     }
 }
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Ssiroo · Jul 29, 2014 at 04:20 PM

I was wrong,ignoring wasn't the case.All the functions are being called correctly,it's just that my script is wrongly written ,Im still trying to figure out how to fix it.

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 FranciscoFontes · Jul 29, 2014 at 04:17 PM

[Sorry for my english] To make sure that the unity ignoring the function ... Debug.Log("w") After that it is easier to know which way the program is being run

 function w()
     {
         canShoot=false;
         yield WaitForSeconds(Random.Range(minShoot,maxShoot));
         canShoot=true;
     Debug.Log("w");
     
     }
      
     function Shoot()
     {
     Debug.Log("Shoot");
             var rocketInstance : Transform;
             rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
             audio.PlayOneShot(soundEffect);
             w();
     }
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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AI Shooting through walls. 1 Answer

AI Evasion help 1 Answer

Animation not being played! 0 Answers

Shotgun Spread Issue 1 Answer

Make tranform follow raycast 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