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 Robomaster · Sep 06, 2012 at 09:04 PM · triggerenemy

How to get Enemy script to target only things in its trigger effect

hello, ive created a script that when an heroe gets within the trigger of a enemy the enemy would then be able to tell what heroe entered and then only damage the heroes that are in its trigger effect. But it doesnt seem to work, unity has given me a few errors, one of them saying that Distance has already been used and i dont really know how to alter my script so that it works. Please help, thanks in advance, heres my script

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAttack1 : MonoBehaviour {
     public GameObject target;
     public GameObject target2;
     public bool Knightbool = false;
      public bool Linemanbool = false;
     public float attackTimer;
     public float coolDown;
         
     // Use this for initialization
     void Start () {
         attackTimer = 0;
         coolDown = 2.0f;
     }
     
     // Update is called once per frame
     void Update () {{
         
         if(attackTimer > 0)
             attackTimer -= Time.deltaTime;
         
         if(attackTimer <0)
             attackTimer = 0;
         
             if(attackTimer == 0) {
             OnTriggerEnter();
                 attackTimer = coolDown;
             
             if(attackTimer == 2.0f){
                 GetComponent<EnemyAttack1>().enabled = false;
         }
     
     }
         }
         }
     
     
     void OnTriggerEnter(Collider Col) {{
             if(Col.gameObject.tag == "Lineman"){
                 target = GameObject.Find ("Lineman");
         Linemanbool = true;
         float distance = Vector3.Distance(target.transform.position, transform.position);
         
         Vector3 dir = (target.transform.position - transform.position).normalized;
         
         float direction = Vector3.Dot(dir, transform.forward);
         
         if(distance < 100.5f) {
             
         
             LinemanHealth lh = (LinemanHealth)target.GetComponent("LinemanHealth");
         lh.AdjustCurrentHealth(-10);
                 }
             }
             
                     
                     if(Col.gameObject.tag == "Knight"){
                         target = GameObject.Find("Knight");
                         Knightbool = true;
                 
                         float distance = Vector3.Distance(target.transform.position, transform.position);
         
         Vector3 dir = (target.transform.position - transform.position).normalized;
         
         float direction = Vector3.Dot(dir, transform.forward);
         
         if(distance < 100.5f) {
             
         
             KnightHealth kh = (KnightHealth)target.GetComponent("KnightHealth");
         kh.AdjustCurrentHealth(-10);
                 }
             }
                             
                             if(Col.gameObject.tag == "Knight" && Linemanbool == true){
                         target = GameObject.Find("Knight");
                         target2 = GameObject.Find("Lineman");
                         float distance = Vector3.Distance(target && target2.transform.position, transform.position);
         
         Vector3 dir = (target && target2.transform.position - transform.position).normalized;
         
         float direction = Vector3.Dot(dir, transform.forward);
         
         if(distance < 100.5f) {
             
         LinemanHealth lh = (LinemanHealth)target2.GetComponent("LinemanHealth");
                     lh.AdjustCurrentHealth(-10);
             KnightHealth kh = (KnightHealth)target.GetComponent("KnightHealth");
         kh.AdjustCurrentHealth(-10);
                 }
             }
                             
                             
                         
                         
                 }
             
             
         
         }
     }
 
         

Comment
Add comment · Show 4
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 SentreStage · Sep 06, 2012 at 09:13 PM 1
Share

Please use the code tags when pasting code in here, or give a link to pastebin or something. It's nearly impossible to tell what your code is even trying to do when it gets improperly formatted because you didnt use the code tags. 99.9% of all of us programmers are going to look at this, and click the back button because they don't need to have to take the time to decipher the jumbled code just to try to help you. However, if you were to edit your post, and fix the code display you might actually get a good number of responses from people that will be willing to help you.

avatar image Piflik · Sep 06, 2012 at 09:15 PM 2
Share

Please format your code correctly. Use the 101010 button (or indent every line 4 spaces). I would do it for you, if it wasn't that many lines...and the empty lines also don't help...

avatar image Robomaster · Sep 06, 2012 at 09:25 PM 1
Share

Sry guys it took me about 30 $$anonymous$$s to edit it becuase of the bugs in unity and i guess i didnt see the formatting errors sry.

avatar image Robomaster · Sep 07, 2012 at 01:31 AM 1
Share

is that better?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by by0log1c · Sep 07, 2012 at 04:16 AM

You are declaring a local variable named 'distance' twice in the OnTriggerEnter method. Idem for 'dir' and 'direction'. I honestly didn't read the code thoroughly to check if you intended to overwrite the previous value or what.

You should probably change all declaration except the first ones, from this:

 float distance = Vector3.Distance(target.transform.position, transform.position);
 Vector3 dir = (target.transform.position - transform.position).normalized;
 float direction = Vector3.Dot(dir, transform.forward);

to a simple assignment, to replace the previously stored values:

 //just an example, note the lack of 'float' and 'Vector3'
 distance = Vector3.Distance(target.transform.position, transform.position);
 dir = (target.transform.position - transform.position).normalized;
 direction = Vector3.Dot(dir, transform.forward);

OR just rename your variables so you don't use the same name twice.

Comment
Add comment · Show 3 · 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 Robomaster · Sep 07, 2012 at 10:08 AM 0
Share

ohhh okay thanks it thought i could only use direction and distance, i didnt know i could change the name. Also unity is saying you cant have && in overands of type, heres the code its saying that for

float distance = Vector3.Distance(target && target2.transform.position, transform.position);

avatar image SentreStage · Sep 07, 2012 at 01:11 PM 1
Share

using Vector3.Distance you can only use one variable in each field.

Example: float distance = Vector3.Distance(target.transform.position, transform.position);

If you need to find the distance between A to B, and from B to C, then you'll need to do two different Vector3.Distance checks accordingly.

avatar image Robomaster · Sep 07, 2012 at 08:58 PM 0
Share

okay so would i just change the float distance to "float distance1"

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

10 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

Related Questions

NavMesh problem it does not respond corectly 0 Answers

destroy a non-trigger object hitting a trigger object? 1 Answer

Enemy doesn't shoot 1 Answer

Object: collider to floor, trigger to player 1 Answer

Distance destroy object 3 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