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 GeorGios1 · Aug 16, 2016 at 03:38 PM · unity 5if statementcondition

If statement is being ignored

Hello all,

I am making an FPS game where in this case, I am programming the enemy to hide when it finds the closest cover point and to shoot me when I get closer to it. However, I want the enemy to shoot in random directions around me to make it more realistic but when the enemy approaches the cover position this specific if statement (line 78) doesn't even execute, but on another enemy and using the same code it does execute. Is there a reason why? Why does Unity3D ignores some lines of code?

pragma strict

 var health = 50;
 var player:GameObject;
 var EnemyDamage = 10;
 var bulletHoleObject:GameObject;
 var ShootPoint:GameObject; //The point where the enemy will shoot
 var effect: Transform;
 var currentTime = Time.time;
 
 var enemyShootCount = 0;
 var IfGetNumberOfShootsCalled = false;
 var numberOfShoots:int;
 
 //weapon
 var clip:int;
 var cover:GameObject;
 
 //Enemy cover system
 var LoockedForTheFirstTime = false;
 
 //Enemy navigation (nav mesh properties)
 var agent: NavMeshAgent;
 
 function DeductPoints(WeaponDamage: int){
     health = health - WeaponDamage;
 }
 
 function Start(){
     clip = 30;
     agent = GetComponent(NavMeshAgent);
 }
 
 function Update () {
 
     var Shot:RaycastHit;
     var PointToPlayer:RaycastHit;
     var UserPlayerPosition:Vector3 = player.transform.position - transform.position;
     //Debug.DrawRay(transform.position, UserPlayerPosition * 1000, Color.red);
     var distanceBetweenEnemyAndPlayer = Vector3.Distance (transform.position, player.transform.position); //The distance between the player and the enemy
     var EnemyAndClosestCoverDistance = Vector3.Distance(transform.position, cover.transform.position); //The distance between the enemy and the cover (We use it to get the closest distance between an enemy and a cover)
 
     // Shooting properties && ammo with reload
     var RandomShoot:Vector3;
 
 
     //covering
     if (distanceBetweenEnemyAndPlayer <= 50 || LoockedForTheFirstTime == true){
         if ((EnemyAndClosestCoverDistance < 50 || LoockedForTheFirstTime == true) && cover.GetComponent(coverProperties).availability == true){ //Checks if the distance between a cover point and a bot is less than 50
 //            transform.position = Vector3.MoveTowards(transform.position, cover.transform.position, Time.deltaTime*7); //moves the bot directly to the cover point
             agent.SetDestination(cover.transform.position);
             LoockedForTheFirstTime = true;
         }
     }
     //---end of covering---
 
     if (distanceBetweenEnemyAndPlayer <= 35){
         gameObject.GetComponent(Renderer).material.color = Color.red;
     }
     else {
         gameObject.GetComponent(Renderer).material.color = Color.white;
     }
 
     //Shooting properties
 
     if ((Time.time - currentTime >= 0.1) && (Physics.Raycast(ShootPoint.transform.position, UserPlayerPosition, Shot)) && distanceBetweenEnemyAndPlayer <= 50){
         Physics.Raycast(ShootPoint.transform.position, UserPlayerPosition, PointToPlayer); //Creates a Ray which points the Player all the time.
         if (PointToPlayer.collider.gameObject.tag == "Player"){ //checks if the PointToPlayer points on the Player (
             //The above if statement is useful because we want the bot to shoot the player only when the PointToPlayer is pointing on the player only.
             if (clip <= 0){
                 currentTime = Time.time + 2.5;
                 enemyShootCount = 0;
                 IfGetNumberOfShootsCalled = false;
                 clip = 30;
             }
             if (IfGetNumberOfShootsCalled == false){
                 numberOfShoots = GetNumberOfShoots();
                 IfGetNumberOfShootsCalled = true;
             }
             if (enemyShootCount <= numberOfShoots){ //`**<---- this if statement is being ignored**`
                 print("Hello");
                 RandomShoot = Vector3(Random.Range(Shot.distance/(-10),Shot.distance/10),Random.Range(Shot.distance/(-10),Shot.distance/10),0);
                 Debug.DrawRay(ShootPoint.transform.position, UserPlayerPosition * 1000, Color.red);
                 enemyShootCount = enemyShootCount + 1;
                 clip = clip - 1;
             }
             if (enemyShootCount == numberOfShoots){
                 currentTime = Time.time + 0.5;
                 enemyShootCount = 0;
                 IfGetNumberOfShootsCalled = false; //enabling access to the second if statement, so we can call the GetNumberOfShoots function
             }
         }
     }
 
     //----End of shooting properties---
 
     //Actual shooting
     if(Physics.Raycast(ShootPoint.transform.position, UserPlayerPosition - RandomShoot, Shot) && Time.time - currentTime >= 0.1 && distanceBetweenEnemyAndPlayer <= 50){
         Physics.Raycast(transform.position, UserPlayerPosition, PointToPlayer); //Creates a Ray which points the Player all the time.
         if (PointToPlayer.collider.gameObject.tag == "Player"){ //checks if the PointToPlayer points on the Player (
             //The above if statement is useful because we want the bot to shoot the player only when the PointToPlayer is pointing on the player only.
             var particleClone = Instantiate(effect, Shot.point, Quaternion.FromToRotation(Vector3(0,0,0),Shot.normal));
             Destroy(particleClone.gameObject, 2);
 
             var bulletHole = Instantiate(bulletHoleObject, Shot.point, Quaternion.FromToRotation(Vector3.up, Shot.normal));
             Destroy(bulletHole.gameObject, 60);
 
             GetComponent.<AudioSource>().Play();
             print(RandomShoot);
             if (Shot.collider.gameObject.tag == "Player"){
                 Shot.transform.SendMessage("UserPlayerHealth" ,EnemyDamage ,SendMessageOptions.DontRequireReceiver);
             }
             currentTime = Time.time;
         }
     }
     //----End of actual shooting----
 
     if (health <= 0){
         Destroy(gameObject);
     }
 }
 
 function GetNumberOfShoots(){
     return Random.Range(5,15);
 }






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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by tanoshimi · Aug 16, 2016 at 03:55 PM

If you're not sure whether you've found a bug in Unity or in your code, it's 99.9999% likely to be in your code. What makes you think the if statement is not being executed? Are you expecting it to return true or false? Test your code by using the following:

 Debug.Log("enemyShootCount is " + enemyShootCount.ToString()  + " and numberOfShoots is " + numberOfShoots.ToString());
 if (enemyShootCount <= numberOfShoots){ 
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 GeorGios1 · Aug 16, 2016 at 05:46 PM 0
Share

It doesn't execute when I move closer to the other player.In that case, the RandomShoot should be updated but it stays on 0,0,0. Also, the piece of code between the //Actual shooting and //----End of actual shooting---- doesn't execute well. I mean that I won't hear the shooting sound from this piece of code:

 GetComponent.<AudioSource>().Play();

as many times as the enemyShootCount number. For example, the numberOfShoots will store 8 but, I will listen to the shoot sound only 3 times. That's the issue I am currently facing. Also, I tried to convert Javascript to C# because C# compiles faster than Javascript, I still have the same error.

Sorry for the bad English but English is not my first language.

I appreciate your help.

avatar image NoseKills GeorGios1 · Aug 16, 2016 at 07:03 PM 0
Share

If Unity randomly ignored lines of code, it would be pretty pointless to try to make anything with it.

That print("Hello"); is the only line of console output in your code. How can you know that you even get to that line (on the times you think it's ignored)? $$anonymous$$aybe shootCount or numberOfShoots get wrong values sometimes? $$anonymous$$aybe you don't even get inside the (2?) enclosing ifs? $$anonymous$$aybe a raycist fails sometimes.

All you can do is debug those things.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

If conditions doesn't work?!? 0 Answers

The If statement condition is false but the if statement stills executes 1 Answer

Why 2d jump not working on touch? 0 Answers

Is There A Way To Make If Statement Condition Shorter 3 Answers

How do i make a sword only do damage to an enemy when the left mouse button is clicked? 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