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 samaxi · Feb 02, 2014 at 03:12 PM · javascriptmovementrigidbodyshooting

Unity3D Ai wont shoot

So my AI enemy wont shoot at the player some of the time, some of my enemy prefabs wont shoot at the player they just look at it. I've tried a whole bunch of things but nothing has worked. Please help, if you get this AI working your name will go into my game credits.

My AI script (contains shooting and walking)

 var distance;
 var target : Transform;    
 var lookAtDistance = 15.0;
 var stopDistance = 3;
 var moveSpeed = 5.0;
 var damping = 6.0;
 
 var damage:float = 17;
 var range = 100.0;
 var force = 10.0;
 var muzzleFlash : Renderer;
 var muzzleLight : Light;
 
 
 var Health = 100;
 var deadReplacement : Transform;
 var dieSound : AudioClip;
 
 static var isDead = false;
 
 static var allowfire : boolean = true;
 
 function Start (){
 muzzleFlash.enabled = false;
 muzzleLight.enabled = false;
 allowfire = true;
 }
 
 function Update () {
     target = GameObject.FindWithTag("Player").transform;
     distance = Vector3.Distance(target.position, transform.position);
  
     if((distance < lookAtDistance)){
     lookAt ();
     }
     if(allowfire == true){
     fire();
 }
 }
 
  
 function lookAt (){
     var rotation = Quaternion.LookRotation(target.position - transform.position);
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
     transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
     animation.Play("walk");
 }
 
 function ApplyDammage (TheDammage : int)
 {
     Health -= TheDammage;
     
     if(Health <= 0)
     {
         Dead();
     }
 }
 
 function Dead () {
     // Destroy ourselves
     Destroy(gameObject);
     Destroy(transform.parent.gameObject);
     
     // Play a dying audio clip
     if (dieSound)
     AudioSource.PlayClipAtPoint(dieSound, transform.position);
 }
 
 static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
     dst.position = src.position;
     dst.rotation = src.rotation;
     
     for (var child : Transform in dst) {
         // Match the transform with the same name
         var curSrc = src.Find(child.name);
         if (curSrc)
             CopyTransformsRecurse(curSrc, child);
     }
 }
 
 function fire(){
 allowfire = false;
 audio.Play();
         
 var direction = transform.TransformDirection(Vector3.forward);
 var hit : RaycastHit;
  
 // Did we hit anything?
 if (Physics.Raycast (transform.position, direction, hit, range)) {
 Debug.DrawRay(transform.position, direction * range, Color.green);
 // Apply a force to the rigidbody we hit
 if (hit.rigidbody)
 hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
 hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
 }
 
 muzzleFlash.renderer.enabled = true;
 muzzleLight.enabled = true;
 yield WaitForSeconds (0.04);
 muzzleFlash.renderer.enabled = false;
 muzzleLight.enabled = false;
 
 
 yield WaitForSeconds(0.3);
 allowfire = true;
 }
 
 function OnTriggerEnter(hit:Collider){
 if(hit.tag == "Player"){
 hit.transform.SendMessage("Damage",damage);
 }
 }

 
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
0

Answer by robertbu · Feb 02, 2014 at 03:28 PM

This problem is on line 21. You've made 'allowfire' static. This mean that all AI enemies share a single value for this variable, so only one AI enemy at a time will be able to fire. Since you call 'fire()' every frame, the first AI called will be the one that fires. Since the AI enemy scripts will be called in some order, the first one called will be the same for each firing resulting other AI enemies not firing at all.

I'm not sure what your intended behavior is here or how many AI enemies you have, so I cannot propose a fix. If you just remove 'static', all of your AI enemies will fire, but firing will overlap.

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 samaxi · Feb 02, 2014 at 03:47 PM 0
Share

O$$anonymous$$G thank you, the reason it was static is cause I copied the players shooting script over but made some changes and forgot that. thank you so much

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

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

AI walk through solid walls 1 Answer

Rigidbody Collision Help 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