• 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 TheIncognito · May 18, 2014 at 03:41 AM · nested

Enemy AI doesn't shoot!

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAi : MonoBehaviour {
 
     public GameObject Target;
 
     public float minDist = 20;
     public float maxDist = 10;
 
     public Transform FirePoint;
 
     public Rigidbody Bullet;
 
     float Health = 100f;
     float Damage = 25;
 
     public void InflictDamage() {
         Health -= Damage;
     }
 
     // Use this for initialization
     void Start () {
         Target = GameObject.FindGameObjectWithTag ("Player");
     }
     
     // Update is called once per frame
     void Update () {
         transform.LookAt(Target.transform.position);
 
         if (Health == 0) {
             Destroy(gameObject);
                 }
 
         if(Vector3.Distance(transform.position,Target.transform.position) >= minDist){
             
             transform.position += transform.forward * 4 *Time.deltaTime;
             
             
             
             if(Vector3.Distance(transform.position,Target.transform.position) <= maxDist)
             {
                 ShootAtHere();
             } 
         }
     }
 
     void ShootAtHere() {
 
         Rigidbody BulletSpawn;
 
         BulletSpawn = Instantiate (Bullet, FirePoint.position, FirePoint.rotation)as Rigidbody;
         BulletSpawn.AddForce (transform.forward * 1000);
 
     }
 }

There don't seem to be any Syntax errors, so I'm clueless!

Comment
Add comment · Show 9
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 getyour411 · May 18, 2014 at 02:31 PM 0
Share

Your bullet is appearing at Firepoint.position which doesn't appear to have any relation to the location of the Enemy this script is attached to.

avatar image Nick4 · May 18, 2014 at 03:24 PM 0
Share

I haven't read the whole thing but why is $$anonymous$$Distance is bigger than maxDistance?

avatar image TheIncognito · May 18, 2014 at 03:37 PM 0
Share

The Enemy is holding a gun, thats has a Empty, which is the FirePoint.

and $$anonymous$$inDist is the $$anonymous$$imum dist the Enemy will be to follow you, max is just when he is supposed to start shooting.

also @getyour411 is The Player Tagged player? Cause the default FPSControllers don't have that tag anymore

avatar image getyour411 · May 18, 2014 at 03:41 PM 0
Share

During play, do you see a new gameobject (the instantiated bullet) appear in your hierarchy? If so, pause it and look at its location

avatar image TheIncognito · May 18, 2014 at 03:49 PM 0
Share

I've tested it, It only appears when I change the if(Vector3.Distance(transform.position,Target.transform.position) <= maxDist) to if(Vector3.Distance(transform.position,Target.transform.position) >= maxDist)

but when I do that, I check in the editor, he shoots them from the gun no problem

Show more comments

1 Reply

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

Answer by Kiwasi · May 19, 2014 at 05:11 AM

Glad you found the solution (in comments). Just to clarify what is happening:

You have a nested if function that means the second if will never get executed

For line 35 to be true the distance must be greater then minDist (20)

For line 41 to be true the distance must be less then maxDist (10)

Therefore line 43 ShootAtHere() will never be executed.

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 Burla · May 19, 2014 at 07:09 AM 0
Share

Glad you took the time to make an answer out of the comments. I see lots of posts being figured out in the comments and no answer is posted. Therefore the Unity Forums Bot will keep showing the post and people won't easily find the answer on a later point. Personally I do the same most of the time.

Have a nice day! +1

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

c# 2D array error: need Help! 1 Answer

Can we edit code of objects in hierarchy directly? 1 Answer

Add a new Array List to an Array List. 1 Answer

Nested for-loop freezing Unity 1 Answer

Good idea to use nested canvases for children of scrollable lists? 2 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