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 /
  • Help Room /
avatar image
0
Question by Crawdad · Feb 15, 2017 at 11:57 PM · else if

Else If running even though conditions not met

Okay, I've been staring at this thing for a long time with no success, so any help will be appreciated. In the code below, I have an enemy character that patrols a spot until the player comes within distance of the raycast. While the player is hit by the raycast, the enemy should stop patrolling and start shooting at the player. The problem I'm running into is that the enemy unit continues patrolling between shots. He will stop, shoot, walk again until the timer runs out, stop, shoot, etc. I feel like something about the timer I've set up is messing with the raycast since the enemy shouldn't patrol while the player is within range, but I can't for the life of me figure out how to fix it. I'm a total noob, so simple answers will be appreciated.

 void Update () {
         playerPos = GameObject.FindGameObjectWithTag ("Player").transform.position;
         direction = (playerPos - (new Vector2 (transform.position.x, transform.position.y))).normalized;
         hit = Physics2D.Raycast (transform.position, direction, bulletDistance);
         tempBulletsTimer -= Time.deltaTime;
         Debug.Log (hit);
         if (hit.collider != null && hit.collider.tag == Tag.Player && tempBulletsTimer <= 0) {
             Instantiate (bullet, transform.position, transform.rotation);
             Debug.DrawLine (transform.position, playerPos, Color.red, .01f);
             tempBulletsTimer = 1;
         } else if (hit.collider == null || hit.collider.tag != Tag.Player) {
             transform.Translate (Patrol ());
         }
     }
Comment
Add comment
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

2 Replies

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

Answer by Crawdad · Feb 17, 2017 at 08:15 PM

I managed to figure out a better way to approach this one that solved my problem. Thanks for the input.

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 IgorAherne · Feb 16, 2017 at 01:46 AM

Check this:

     Transform playerTransform;
 
     void Start() {
         //start runs once game object (to which this script is attached) is bourn.
         //Runs only once, so use this expensive "Find" function once, and not every frame
         playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
 
     void Update() {
         //you had something weird going on with Vector2 here:
         direction = (playerTransform.position - transform.position).normalized;
 
         hit = Physics2D.Raycast(transform.position, direction, bulletDistance);
 
         tempBulletsTimer -= Time.deltaTime;
 
         if (hit.collider == null || hit.collider.tag != Tag.Player) {
             transform.Translate(Patrol());
             return;
         }
 
         //otherwise, we still didn't return. Conditions must have been met!
         //we are now sure that "hit" is NOT null and the player has correct tag.
 
         if (tempBulletsTimer <= 0) {
 
             Instantiate(bullet, transform.position, transform.rotation);
             Debug.DrawLine(transform.position, playerPos, Color.red, .01f);
             tempBulletsTimer = 1;
         } 
 
     }//end Update()
 }
Comment
Add comment · Show 1 · 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 Crawdad · Feb 16, 2017 at 02:30 PM 0
Share

Thanks for suggestions, Igor. I tried this setup and, although better thought out than what I had, the result was ultimately the same. The only modifications I had to make to your script was to keep the playerTransform variable in the update section so that the enemy shot towards his current location, and not his location at the beginning of the scene. It still seems like the raycast returns null during a portion of the function even while the player is well within the raycast distance. If you have any other ideas, please let me know. Thanks again.

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

92 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 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 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 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

Add sprint Key 2 Answers

Looking to overlook arguments based on parameters elsewhere in the code. (C#) 0 Answers

Flipping Switches 1 Answer

Cursor Lock Code w/ if, else if, and else Statement Errors 1 Answer

IF Else Statement being ignored ,the else if condtion is being ignored and playing first IF condition for all game objects. 0 Answers


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