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 ltrout1999 · Jan 07, 2016 at 09:20 PM · range

How to have enemy only shoot when they are within a certain range?

I have my Enemy Tank setup to only shoot when they are within 25 units of the Player Tank, but I want the Enemy Tank to also only shoot if they are greater than 10 Units and Less than 25 units of the Player Tank (since the bombs do damage based on how close you are, it wouldn't seem realistic to have the enemy shoot if it's going to cause damage on the enemy as well). Here is my class setup currently, and it doesn't seem to be working too well. (the main part is at the end of the code)

 using UnityEngine;
 using System.Collections;
 using PlayerInfo;
 using Shell;
 
 namespace EnemyInfo
 {
     public class EnemyMovement : MonoBehaviour 
     {
         public Transform player;
         public Transform EnemyTank;
         public int AttackRange = 35;
         public bool inRange;
         public int TooClose = 10;
 
         NavMeshAgent agent;
         static PlayerHealth pHealth;
         static EnemyHealth eHealth;
         Rigidbody Rigidbody;
 
         void Awake ()
         {
             player = GameObject.FindWithTag ("Player").transform;
             pHealth = player.GetComponent<PlayerHealth> ();
             eHealth = GetComponent<EnemyHealth> ();
             Rigidbody = GetComponent<Rigidbody> ();
         }
 
         void OnEnable ()
         {
             Rigidbody.isKinematic = false;
         }
 
         void OnDisable ()
         {
             Rigidbody.isKinematic = true;
         }
 
         void Update ()
         {
             NavMeshAgent agent = GetComponent<NavMeshAgent> ();
 
             if (pHealth.CurrentHealth > 0 && eHealth.CurrentHealth > 0) 
             {
                 agent.SetDestination (player.position);
                 transform.LookAt (player.position);
             } 
             else 
             {
                 agent.enabled = false;
             }
 
             if (Vector3.Distance (player.position, EnemyTank.position) < AttackRange) {
                 if (Vector3.Distance (player.position, EnemyTank.position) > TooClose)
                 {
                     inRange = true;
                 }
             } 
             else if (Vector3.Distance (player.position, EnemyTank.position) > AttackRange)
             {
                 inRange = false;
             }
         }
     }
 }
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 ltrout1999 · Jan 07, 2016 at 09:20 PM 1
Share

@dhore , since you help me with everything else I thought I would tag you :)

1 Reply

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

Answer by dhore · Jan 08, 2016 at 02:03 AM

I made a few changes to your code, hopefully it works...

Changes start at lines 20, 32 & 61 and all have comments explaining them.

 using UnityEngine;
 using System.Collections;
 using PlayerInfo;
 using Shell;
 
 namespace EnemyInfo
 {
     public class EnemyMovement : MonoBehaviour 
     {
         public Transform player;
         public Transform EnemyTank;
         public int AttackRange = 35;
         public bool inRange;
         public int TooClose = 10;
 
         NavMeshAgent agent;
         static PlayerHealth pHealth;
         static EnemyHealth eHealth;
         
         // change the name of the Rigidbody variable to have a lower case 'r' so that the compiler won't confuse it with the Type Rigidbody
         // (variable type names are reserved words, so you should never use these words with the exact capitalization for a variable name)
         // remember to change the name where you use it below too
         Rigidbody rigidbody; 
 
         void Awake ()
         {
             player = GameObject.FindWithTag ("Player").transform;
             pHealth = player.GetComponent<PlayerHealth> ();
             eHealth = GetComponent<EnemyHealth> ();
             rigidbody = GetComponent<Rigidbody> ();
             
             // EnemyTank is never initialized (unless you did that from the inspector)
             // assuming this script is attached to the EnemyTank, if so then this would work:
             EnemyTank = transform;
         }
 
         void OnEnable ()
         {
             rigidbody.isKinematic = false;
         }
 
         void OnDisable ()
         {
             rigidbody.isKinematic = true;
         }
 
         void Update ()
         {
             NavMeshAgent agent = GetComponent<NavMeshAgent> ();
 
             if (pHealth.CurrentHealth > 0 && eHealth.CurrentHealth > 0) 
             {
                 agent.SetDestination (player.position);
                 transform.LookAt (player.position);
             } 
             else 
             {
                 agent.enabled = false;
             }
 
             // ok, so this part needs to be changed just a little bit
             // firstly we're going to store the Distance in a variable to avoid multiple calls to the exact same function
             float distance = Vector3.Distance (player.position, EnemyTank.position);
             
             // there's no need to use nested if statements here - you could just check both conditions in the same IF
             // BUT there's an even quicker way to do what you want:
             // inRange will be True if the condition is True - otherwise it will be False
             // therefore just do this:
             inRange = ((distance < AttackRange) && (distance > TooClose));
         }
     }
 }

Let me know if this fixes your problem :)

Comment
Add comment · Show 12 · 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 ltrout1999 · Jan 08, 2016 at 02:17 AM 0
Share

Doesn't seem to work. The enemy is still shooting no matter how close he is.

avatar image dhore ltrout1999 · Jan 08, 2016 at 02:20 AM 0
Share

Ok, well which script controls the shooting? Because the above script only seems to control the decision making, but not the actual shooting.

avatar image dhore dhore · Jan 08, 2016 at 06:56 AM 1
Share

@ltrout1999 I know what you mean, but without knowing how all of your code works together I may not be able to give you any exact working code. Though the following code should give you your random percent that you want:

 int missChance = 45; // chance of missing
 float distance = Vector3.Distance (player.position, EnemyTank.position);
 
 int randPercent = Random.Range(1,100); // random number for percent chance
 
 if (randPercent <= missChance)
 {
     // bullet misses
     LaunchForce = 0; // probably wouldn't work - read below
 }
 else
 {
     // bullet hits
     LaunchForce = distance; // may not work either - read below
 }

So yeah, I don't expect that to work.... but it may....

I'm not sure exactly how you want to accomplish hitting & missing the target... will the bullet just fall short? Or do you want it to shoot off to the side? The above code should just make it fall short, but depending on how your bullet collisions and such are handled that could cause lots of unforeseen issues...

Anyway, get back to me if you decide what you need and want some more help - I'm quite happy to help you out :)

Show more comments
Show more comments

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

38 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

Related Questions

Array index out of range(C#) 1 Answer

Any way to increase raycast range? 0 Answers

Get list of enemies in range 1 Answer

I can't get a "victim" (can be seen as enemy) to follow the player within a certain range 0 Answers

Math - How to use a percentage that stays within a range? 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