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 Ssiroo · Jul 29, 2014 at 02:37 PM · javascriptraycastaicolliders

AI Shooting through walls.

Hello ,I made a AI shooting script for my new game but the problem is that the AI ignores the walls and shoots through walls. This is my ai script :

 #pragma strict
 
 public var rocketPrefab : Transform;
 public var barrelEnd : Transform;
 var canShoot : boolean = true;
 var shootRate : float;
 var gun : Transform;
 var soundEffect : AudioClip;
 var player : Transform;
 var enemy : Transform;
 var minShoot : float = 2;
 var maxShoot : float = 4;
 
 function Start ()
 {
 
     e();
     canShoot = true;
 }
 
 function Update ()
 {
     transform.LookAt(player);
     if(canShoot==true)
     {
         Shoot();
     }
 }
 
 function e()
 {
     canShoot=false;
     yield WaitForSeconds(Random.Range(1, 2));
     canShoot=true;
 }
 
 function w()
 {
     canShoot=false;
     yield WaitForSeconds(Random.Range(minShoot,maxShoot));
     canShoot=true;
 }
 
 function Shoot ()
 {
         var rocketInstance : Transform;
         rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
         w();
         audio.PlayOneShot(soundEffect);
 }

What Im trying to do is make the AI shoot only when there is nothing between them. I tried adding a box collider(trigger on) and making it shoot only when the player is inside and it worked but while colliding with other objects it made the game really laggy and it wasn't looking so good. This is the script I made :

 #pragma strict
 
 var ai : Ai;
 
 function Start ()
 {
     ai = transform.parent.GetComponent(Ai);
     ai.enabled = false;
 }
 
 function OnTriggerStay ()
 {
     ai.enabled = true;
 }
 
 function OnTriggerExit ()
 {
     ai.enabled = false;
 }

I also destroyed the object when the AI died using this script :

 var enemy : Transform;
 var ai : Ai;
 var aiDeath : AIDeath;
 var aiDeath1 : AIDeath1;
 var head : Transform;
 var sounds : AudioClip[];
 var nextlevel : NextLevel;
 var enemySight : Transform;
 
 function Start()
 {
     ai = GetComponent(Ai);
     aiDeath = GetComponent(AIDeath);
     aiDeath1 = head.GetComponent(AIDeath1);
     nextlevel = GameObject.FindWithTag("NextLevel").GetComponent(NextLevel);
 }
 
 function OnTriggerEnter(col : Collider)
 {
     if(col.gameObject.tag=="Bullet")
     {
         enemy.animation.Play("BodyShot");
         audio.PlayOneShot(sounds[Random.Range(0, sounds.length)]);
         Destroy (enemySight.gameObject);
         nextlevel.enemies -= 1;
         Destroy (ai);
         Destroy (aiDeath);
         Destroy (aiDeath1);
         Destroy (enemy.animation, 1);
     }
 }


Some help would be awsome! Thank you for your time.

Update :

@Vitor_r - I guess your answer would look kind of like this :

 #pragma strict
 
 public var rocketPrefab : Transform;
 public var barrelEnd : Transform;
 var canShoot : boolean = true;
 var shootRate : float;
 var gun : Transform;
 var soundEffect : AudioClip;
 var player : Transform;
 var enemy : Transform;
 var minShoot : float = 2;
 var maxShoot : float = 4;
 var rayLength = 10000;
 
 function Start ()
 {
 
     e();
     canShoot = false;
 }
 
 function Update ()
 {
     transform.LookAt(player);
     var hit : RaycastHit;
     var forward = transform.TransformDirection(Vector3.forward);
     if(Physics.Raycast(transform.position, forward, hit, rayLength))
     {
         if(hit.collider.gameObject.tag != "Player")
         {
             canShoot = false;
         }
         
         else if(hit.collider.gameObject.tag == "Player")
         {
             canShoot = true;
         }
         }
     
         if (canShoot == true)
         {
             Shoot();
         }
 }
 
 function e()
 {
     canShoot=false;
     yield WaitForSeconds(Random.Range(1, 2));
     canShoot=true;
 }
 
 function w()
 {
     canShoot=false;
     yield WaitForSeconds(Random.Range(minShoot,maxShoot));
     canShoot=true;
 }
 
 function Shoot ()
 {
         var rocketInstance : Transform;
         rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
         w();
         audio.PlayOneShot(soundEffect);
 }

It works,the AI doesn't shoot through the wall anymore but when it sees the player,it shoots like 100 bullets per second.It seems to be ignoring my "w" function somehow, I think.

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
Best Answer

Answer by Vitor_r · Jul 29, 2014 at 02:42 PM

If you wall has a collider, you can raycast towards the player, and if the raycast hits the player it means that the AI can see the player and can shoot.

If the raycast hit the wall, the player isn't visible.

Comment
Add comment · Show 6 · 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 Ssiroo · Jul 29, 2014 at 03:02 PM 0
Share

Updated,it kinda helped.

avatar image Vitor_r · Jul 29, 2014 at 03:59 PM 0
Share

It's shooting a lot of bullets because you are doing a raycast every update and setting canShoot=true; A easy solution would be use another variable for the delay.

And test it before the raycast: #pragma strict

     public var rocketPrefab : Transform;
     public var barrelEnd : Transform;
     var canShoot : boolean = true;
     var shootRate : float;
     var gun : Transform;
     var soundEffect : AudioClip;
     var player : Transform;
     var enemy : Transform;
     var $$anonymous$$Shoot : float = 2;
     var maxShoot : float = 4;
     var rayLength = 10000;
     var shootWait;
      
     function Start ()
     {
      
         e();
         canShoot = false;
         shootWait=false;
     }
      
     function Update ()
     {
         transform.LookAt(player);
         if(!shootWait){
             var hit : RaycastHit;
             var forward = transform.TransformDirection(Vector3.forward);
             if(Physics.Raycast(transform.position, forward, hit, rayLength))
             {
             if(hit.collider.gameObject.tag != "Player")
             {
                 canShoot = false;
             }
              
             else if(hit.collider.gameObject.tag == "Player")
             {
                 canShoot = true;
             }
             }
              
             if (canShoot == true)
             {
                 shootWait=true;
                 Shoot();
             }
         }
     }
      
     function e()
     {
     canShoot=false;
     yield WaitForSeconds(Random.Range(1, 2));
     canShoot=true;
     shootWait=false;
     }
      
     function w()
     {
     canShoot=false;
     yield WaitForSeconds(Random.Range($$anonymous$$Shoot,maxShoot));
     canShoot=true;
     shootWait=false;
     }
      
     function Shoot ()
     {
     var rocketInstance : Transform;
     rocketInstance = Instantiate(rocketPrefab, barrelEnd.position, barrelEnd.rotation);
     w();
     audio.PlayOneShot(soundEffect);
     }
avatar image Ssiroo · Jul 29, 2014 at 05:08 PM 0
Share

Now it doesn't shoot at all :/

avatar image Vitor_r · Jul 29, 2014 at 05:43 PM 0
Share

I changed it in notepad. $$anonymous$$aybe i wrote something wrong. And i can't check it right now because i dont have Unity here, i'll check it later when i get home :)

But you can try figure out why it's not working, i just changed the "Shoot a lot" code and implemented a way to control when the Ai can or cannot shoot.

avatar image Ssiroo · Jul 29, 2014 at 05:51 PM 0
Share

Great,thanks a lot! Im still trying to get it to work but my lack of sleep and headache are makign it impossible for me.

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

22 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

Related Questions

Functions being ignored 2 Answers

AI Evasion help 1 Answer

Animation not being played! 0 Answers

Shotgun Spread Issue 1 Answer

I need an explanation about raycast and collisions 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