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 johnniks · Feb 23, 2016 at 03:54 PM · c#raycastshootingprojectileartificial intelligence

Can't make my AI shoot projectiles with raycast

Hello,

I'm kinda stuck and I need some help in order to make my AI shoot projectiles with the usage of a raycast. The idea is that I want to apply my already existing script for the player (see below) and add it to my AI's shootPlayer function in its own script (see below) but I've never worked with raycasts (nor AIs for that matter) before and I seem at a lose as how to implement my already functional stuff along with an AI and the usage of a raycast.

Player Shooting script: https://gist.github.com/fiskefyren/36781777f24b65a13d39

AI script: https://gist.github.com/fiskefyren/c06ce230837120bad4ea

Thanks in advance :)

Comment
Add comment · Show 16
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 Salmjak · Feb 23, 2016 at 04:33 PM 0
Share

You're doing it right. You might want to change the direction of the raycast though. Use (target.position - AI.position).normalized to get a Vector3 direction between the target and the AI, this way the raycast will always be aimed at the player (although this can mean the AI will shoot backwards so you should probably also turn the AI in this direction).

avatar image johnniks Salmjak · Feb 23, 2016 at 04:38 PM 0
Share

yeah i don't want that... my issues lies more with i'm not sure how i get my AI to even shoot...

avatar image Salmjak johnniks · Feb 23, 2016 at 04:44 PM 0
Share

Just do the same as with the player. Instantiate your bullet or whatever and move it in a direction and have the collision detection and damage calculation on the bullet. I don't even see why you want to use raycast. Do you want to damage the player directly? If you don't you shouldn't use raycast there (you could use it for detection though), but you don't want the AI to turn and shoot at the player, so I don't know why you would even want that. You just want your AI to shoot aimlessly and hope it hits something it sounds like, so then just loop an enumerator "Shoot()" and use a yield return WaitForSeconds() to deter$$anonymous$$e how often the AI should shoot.

Show more comments
Show more comments
avatar image johnniks Salmjak · Feb 23, 2016 at 05:21 PM 0
Share

... but the raycast already works as intended... i'm really confused why you keep talking about it... i just can't figure out how to make it work probably when it detects the player (which it does just fine)...

as for SphereCast or SphereOverlap etc. I've no idea what that is... it's not something I've used before... i just wanted to make something simple where the raycast simply shoots the projectile when the raycast hits the player, that was really all... but for some reason I'm having issues making that happen.

i honestly don't understand why you don't understand what i want to do... i've no idea how else to explain it... but maybe your ideas are better? i wouldn't know, and i sure as heck don't know right now how i would go about doing your suggestions...

avatar image Salmjak johnniks · Feb 23, 2016 at 05:30 PM 1
Share

Again, what are you trying to do? What does "work properly" mean? Apparently it doesn't quite do that since you seem to need help. What isn't working as intended and why? First of, you instantiate your bullets OUTSIDE of the raycast-check. Is this intentional or not? I don't know. If it's not check out the code below. Try this code:

     RaycastHit rayHit;
             if (Physics.Raycast(transform.position, transform.forward, out rayHit)) { //out is a keyword, you can add another var for the distance of the ray
                if(rayHit.transform.gameObject.CompareTag("Player")){ //did we hit the player?
                 
                 isFiring = true;
                 GameObject.Instantiate(laserPrefab, leftFirePoint.position, leftFirePoint.rotation);
             GameObject.Instantiate(laserPrefab, rightFirePoint.position, rightFirePoint.rotation);
                 }
                 /*
                 I'm clueless D:
                 */
                 }
                 Debug.Log("Shooting at player!" + rayHit.collider.gameObject.tag);
             }
             

This will only shoot if you hit the GameObject with the tag "Player". And again use a Coroutine (as I linked to previously) for looping the shooting. Use something like: IEnumerator Shoot(){ while(isFiring){ GameObject.Instantiate(bulletPrefab); yield return new WaitForSeconds(cooldown);}}

Show more comments
Show more comments

0 Replies

· Add your reply
  • Sort: 

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

114 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 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 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

Shoot towards target 0 Answers

making a boomerang effect in a 2D enviorment 1 Answer

Bullet goes sideways when the player gets hit 1 Answer

Projectile trajectory based on angle 0 Answers

Cant Kill enemy with Raycast C# (SOLVED) 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