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 /
avatar image
-1
Question by DayyanSisson · Aug 17, 2011 at 12:41 AM · targetfindgameobjectswithtagbce0023physics.overlapsphere

AI Turret Targeting System

I have a modified turret script and there is a major problem: it's designed to only look at one target. I modified this script to find objects with a certain tag. But it doesn't do it. Here's the script:

 var distanceTillShoot : float;
 var LookAtTarget : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
 var damp : float = 5.0;
 var bulletPrefab : GameObject;
 var savedTime = 0;
 
 function Update ()
 {
      if(LookAtTarget)
      {
           var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 
           transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
           var seconds : int = Time.time;
           var oddeven = (seconds % 9999);
           if(oddeven) 
               Shoot(seconds);
           transform.LookAt(LookAtTarget);
      }
 
 }
 
 function Shoot(seconds)
 {
  var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
    if (distance <= distanceTillShoot){
      if(seconds!=savedTime)
      {
           var bullet = Instantiate(bulletPrefab,transform.Find("TSP").transform.position , transform.rotation);
      }
 
 
      savedTime=seconds;
     }
 }

And I'm getting this error message:

`BCE0023: No appropriate version of 'UnityEngine.Transform.LookAt' for the argument list '(UnityEngine.GameObject[])' was found.`

I think the problem is that it's trying to transform the position of a Transform, not a GameObject, but I need it to transform the position of a GameObject with Tag. I don't know how to do this. Thanks in advance.

Update

Okay, I've tried a different approached with my own script:

 #pragma strict
 @script ExecuteInEditMode()
 @script AddComponentMenu ("Void Runners : Towers/AI/Friendly/Friendly Turrets")
 class FriendlyTurrets extends MonoBehaviour {
 }
 
 var primaryRange : float = 100;
 var specialRange : float = 500;
 private var target : Transform;
 var damp : float = 2.0;
 var primary : GameObject;
 var primaryMuzzleFlash : GameObject;
 var primaryAnimation : Animation;
 var primaryFiringSound : AudioClip;
 var special : GameObject;
 var specialMuzzleFlash : GameObject;
 var specialAnimation : Animation;
 var specialFiringSound : AudioClip;
 var primaryFireRate : float = 0.1;
 var specialFireRate : float = 30;
 private var nextFire : float = 0.0;
 private var savedTime : float = 0;
 
 
 function Update ()
 {
           var rotate = Quaternion.LookRotation(target.position - transform.position); 
           transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
           transform.LookAt(target);
 }
 
 
 function OnCollisionEnter () {
 
         var colls = Physics.OverlapSphere(transform.position, primaryRange);
     for (var col: Collider in colls){
         target = col.CompareTag("Enemy");
         if (col.CompareTag("Enemy")){
             var distance = Vector3.Distance(col.transform.position, transform.position);
                 NormalAttack();            
             }
             else 
             if (distance <= specialRange){
                 SpecialAttack();
             }
     }
     
 }
 
 
 function NormalAttack () {
     
     if(Time.time > nextFire) {
         nextFire = Time.time + primaryFireRate;    
     Instantiate (primary, transform.position, transform.rotation);           
     Instantiate (primaryMuzzleFlash, transform.position, transform.rotation);
     Animation.Play(primaryAnimation);
     audio.PlayOneShot(primaryFiringSound);           
     }
     
 }
 
 function SpecialAttack () {
     
     if(Time.time > nextFire) {
         nextFire = Time.time + specialFireRate;    
     Instantiate (special, transform.position, transform.rotation);           
     Instantiate (specialMuzzleFlash, transform.position, transform.rotation);
     Animation.Play(specialAnimation);
     audio.PlayOneShot(specialFiringSound);            
     }
     
 }

I can't test this approach because I get this error:

Friendly Turret.js(37,32) BCE0022: Cannot convert 'boolean' to 'UnityEngine.Transform'

Well line 37 is:

     target = col.CompareTag("Enemy");

So it's treating the "target" variable as a boolean at first, then gives an error when I use it as a Transform. Well, "target" is set as an empty Transform in the variable list. I'm not sure what I wrote wrong.

Since I can't test this, I don't even know if this will work or not, because I have never tried this approach and I haven't seen anyone who has. So if I fix that error, would this script even work? If it won't work, I need to know why to see if I can change it. Your help would be greatly appreciated.

Comment
Add comment · Show 1
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 mgear · Aug 17, 2011 at 12:55 AM 0
Share

It seems "var LookAtTarget : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");" Returns array/multiple objects, so which one of those you want to look at?

Otherwise it should work as: "transform.LookAt(LookAtTarget.transform);"

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by DayyanSisson · Oct 29, 2011 at 05:23 AM

Okay, I found an answer myself. I just added this to the top of the Update function:

 target = GameObject.FindWithTag("Human Fighter").transform;

Here's the whole script:

 var range : float = 100;
 var target : Transform;
 var damp : float = 2.5;
 var bulletPrefab : GameObject;
 var fireRate : float = 10.0;
 private var nextFire : float = 0.0;
 private var savedTime : float = 0;
 
 function Update ()
 {
      target = GameObject.FindWithTag("Scorpion").transform;
      
      if(target)
      {
           var rotate = Quaternion.LookRotation(target.position - transform.position); 
           transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
           var seconds : float = Time.time;
           var oddeven = (seconds);
           if(oddeven) 
               Shoot(seconds);
           transform.LookAt(target);
      }
 }
 
 function Shoot(seconds)
 {
  var distance = Vector3.Distance(target.transform.position, transform.position);
    if (distance <= range){
      if(seconds!=savedTime && Time.time > nextFire)   {
 nextFire = Time.time + fireRate;
           var bullet = Instantiate(bulletPrefab,transform.Find("TSP").transform.position , transform.rotation);
      }
      savedTime=seconds;
     }
 }
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
2

Answer by roamcel · Aug 17, 2011 at 05:48 AM

Your problem is based what @mgear spotted, but there's possibly a few other flaws. FindGameObjectsWithTag returns an array, so you need to change your code appropriately.

For a start, add two variables

var enemieslist : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
var currentenemyindex = 0;

and change LookAtTarget

var LookAtTarget : GameObject;

Then you will need another function

function setcurrentenemy() {
  //scan enemieslist and set currentenemyindex to the value of interest
}

then update your update function

...
function Update ()
{
     //in your original code the enemy list is only populated once
     //with this instruction here, it'll get updated always, but since find with tag
     //is a slow function, you'll need to move it to somewhere more appropriate
     enemieslist = GameObject.FindGameObjectsWithTag("Enemy");
     if(enemieslist.Length>0)  //possibly a meaningless check at this point
     {
       setcurrentenemy();
       LookAtTarget = enemieslist[currentenemyindex];
     ...
     transform.LookAt(LookAtTarget.transform);
Comment
Add comment · Show 18 · 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 DayyanSisson · Aug 17, 2011 at 05:01 PM 0
Share

Do I completely replace my update function with this?

avatar image roamcel · Aug 17, 2011 at 06:39 PM 0
Share

function Update ()

{

  //in your original code the enemy list is only populated once
  //with this instruction here, it'll get updated always, but since find with tag

  //is a slow function, you'll need to move it to somewhere more appropriate

  enemieslist = GameObject.FindGameObjectsWithTag("Enemy");

  if(enemieslist.Length>0)  //possibly a meaningless check at this point

  {
    setcurrentenemy();

    LookAtTarget = enemieslist[currentenemyindex];

       var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 

       transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 

       var seconds : int = Time.time;

       var oddeven = (seconds % 9999);

       if(oddeven) 

           Shoot(seconds);

       transform.LookAt(LookAtTarget.transform);
  }

}

avatar image DayyanSisson · Aug 17, 2011 at 06:52 PM 0
Share

Wait, I'm still confused about the set enemy target. I added that above the function update. I'm not sure what to put in it. Please explain. (thanks for the function update clear up)

avatar image DayyanSisson · Aug 19, 2011 at 12:49 AM 0
Share

Ok, I tried what you did and this is how my script came out:

 var distanceTillShoot : float;
 var LookAtTarget : GameObject;
 var damp : float = 5.0;
 var bulletPrefab : GameObject;
 var savedTime = 0;
 var enemieslist : GameObject[] = GameObject.FindGameObjectsWithTag("Enemy");
 var currentenemyindex = 0;
 
 function setcurrentenemy() {
   //scan enemieslist and set currentenemyindex to the value of interest
 }
 
 function Update ()
 
 {
 
  //in your original code the enemy list is only populated once
  //with this instruction here, it'll get updated always, but since find with tag
 
  //is a slow function, you'll need to move it to somewhere more appropriate
 
  enemieslist = GameObject.FindGameObjectsWithTag("Enemy");
 
  if(enemieslist.Length>0)  //possibly a meaningless check at this point
 
  {
    setcurrentenemy();
 
    LookAtTarget = enemieslist[currentenemyindex];
 
       var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position); 
       transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp); 
       var seconds : int = Time.time;
       var oddeven = (seconds % 9999);
       if(oddeven) 
 
           Shoot(seconds);
 
       transform.LookAt(LookAtTarget.transform);
  }
  
 function Shoot(seconds)
 {
  var distance = Vector3.Distance(LookAtTarget.transform.position, transform.position);
    if (distance <= distanceTillShoot){
      if(seconds!=savedTime)
      {
           var bullet = Instantiate(bulletPrefab,transform.Find("TSP").transform.position , transform.rotation);
      }
 
 
      savedTime=seconds;
     }
 }
avatar image DayyanSisson · Aug 19, 2011 at 01:00 AM 0
Share

And the errors I'm getting with the script have to do with the function Shoot(seconds), not the rest of the script, and the errors make no sense. Here's what I'm getting:

Assets/Void Runners/Scripts/Turret AI.js(42,10): BCE0044: expecting (, found 'Shoot'.

That makes no sense because Shoot HAS to come first for it to work. Here's another one:

Assets/Void Runners/Scripts/Turret AI.js(42,24): UCE0001: ';' expected. Insert a semicolon at the end.

That still makes no sense because it's a function and doesn't need a semi-colon.

And then line 43 supposedly has two errors: Unexpected token:var, & another semi-colon missing.

None of this makes any sense to me. Could someone help? (The script I'm using is below this comment)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

HELP! i want to write to look at the target and give a force to follow 3 Answers

FindWithTag in certain range 2 Answers

Why is FindGameObjectsWithTag ignoring my Physics.OverlapSphere? 1 Answer

c# Get target gameobject from a list 1 Answer

TwoBoneIKConstraint Target wont update from script 0 Answers


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