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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by schult777 · Mar 20, 2014 at 07:10 PM · raycastaitarget

drone to target enemies

I am working on a script for a drone that will be along side the player. It will target multiple enemies and destroy them. Does anyone know of any good tutorials I might learn from? I have a script I have written but it is not working and I am not sure why. I would like to find out why it is not working and how to fix it. Below is the script.

 using UnityEngine;
 using System.Collections;
 
 public class eshooter : MonoBehaviour 
 {
     LineRenderer line;
     public GameObject explosionA;
     public AudioClip boom;
     public GameObject explosionB;
     public GameObject explosionC;
 
 
         // Use this for initialization
     void Start () 
     {
         line = gameObject.GetComponent<LineRenderer>();
         line.enabled = false;
     }
     
     // Update is called once per frame
     void update ()
     {
         Vector3 fwd = transform.TransformDirection(Vector3.forward);
         if (Physics.Raycast(transform.position, fwd, 2500))
     {
             StartCoroutine("FireLaser", 0.25F);
             audio.Play();
                     }
     }
     IEnumerator FireLaser(float lengthOfTime)
     {
         line.enabled = true;
         var t = Time.time;
         
         while(Input.GetButton("Fire1") && Time.time - t < lengthOfTime)
             
         {
             Ray ray = new Ray(transform.position, transform.forward);
             RaycastHit hit;
             
             line.SetPosition(0, ray.origin);
             
             if(Physics.Raycast(ray, out hit, 2500))
             {
                 line.SetPosition(1, hit.point);
                 if  (hit.collider.tag == "Asteroid")
                 {
                     Instantiate(explosionA, hit.point, transform.rotation);
                     Destroy(hit.transform.gameObject); 
                     audio.PlayOneShot(boom, 1F);
                 }
                 else if  (hit.collider.tag == "Missile")
                 {
                     Instantiate(explosionB, hit.point, transform.rotation);
                     Destroy(hit.transform.gameObject); 
                     audio.PlayOneShot(boom, 1F);
                 }
                 else if  (hit.collider.tag == "Ufo")
                 {
                     Instantiate(explosionC, hit.point, transform.rotation);
                     Destroy(hit.transform.gameObject); 
                     audio.PlayOneShot(boom, 1F);
                 }
             }
             else 
                 line.SetPosition(1, ray.GetPoint(2500));
             
             yield return null;
         }
         line.enabled = false;
         
     }
 }
 
Comment
Add comment · Show 3
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 AlucardJay · Mar 20, 2014 at 08:15 PM 1
Share

Update with a capitol U

avatar image schult777 · Mar 22, 2014 at 12:35 AM 0
Share

I'll look into that and see what happens. thank you.

avatar image schult777 · Mar 22, 2014 at 01:08 AM 0
Share

That did not do anything for the script. do you know of any tutorials where I can learn what is wrong with my script?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SkaredCreations · Mar 22, 2014 at 01:49 AM

What's the issue you're having? The only bug in your code is that the function is not "update" but "Update" (lettercase counts in C#), after calling the function with the correct name it just works (I tried myself with 2 cubes). If it's not instantiating the explosion objects then it means:

a) raycast in FireLaser is not hitting anything (have your targets a collider attached?)

b) you're not hitting the button/key "Fire1"

c) raycast in FireLaser is hitting something that has not tag "Asteroid", "Missile", "Ufo"; try to add a debug statement after the check for tag "Ufo", like this: else Debug.Log("hit " + hit.name);

d) the object "eshooter" is not facing the direction you think so it's not hitting anything (may be wrong rotation?), but in this case you should see the effect of line 66 (line.SetPosition...).

You can add the following function to see in the Scene panel if it's facing the correct direction and if actually hits something (though remember that for Raycast to work you need colliders attached on your target objects):

 void OnDrawGizmos () {
     Debug.DrawRay(transform.position, transform.forward * 100);
 }
 
Comment
Add comment · 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
0

Answer by davidlee1 · Oct 02, 2018 at 12:41 PM

Awesome. Keep us updated about improvements and i am also modeling a 3D camera on drone. Also trying to add some kind of eye to the capsule so the eye will rotate with the camera. Something like a webcam style. Yesterday purchased a Drone as recommended in an article i read about upcoming drones of 2019. Have a Great Day! :)

Comment
Add comment · 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

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

24 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

Related Questions

Multiple targeting help 1 Answer

Finding RayCastHit's Origin Position 2 Answers

Implementing BUG2 algorithm 0 Answers

Using Raycast to "Lock On" 1 Answer

AI Field of vision 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