- Home /
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;
}
}
That did not do anything for the script. do you know of any tutorials where I can learn what is wrong with my script?
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);
}
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! :)
Your answer
Follow this Question
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