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 baltraven · Aug 26, 2013 at 01:30 PM · targetauto

Auto-targeting help

Hi,

The following script is what I use in an attempt to have an 'auto-target' system. What happens is:

  1. Finds all game objects tagged 'Enemy'.

  2. Calculates the distance between transform(the player) and the game object.

  3. If the distance is less than or equal to 5, change some values.

What I expect to happen:

  1. When player is in range of game object tagged 'Enemy', the player's variable 'target' changes to the object within range.

  2. When player leaves that range, the target goes to null.

  3. When the player reenters ANY range of a object with tag 'Enemy', the player's variable 'target' becomes that object.

What actually happens:

  1. When a player enters a range of an object with tag 'Enemy', the player's variable 'target' becomes that object. GOOD

  2. When the player exits the range, the variable goes to null. GOOD

  3. Reentering the same object's range, rechanges the variable to that object. GOOD

  4. BUT, it only works with one gameobject with tag 'Enemy'. Entering a different object that is identical to the other, will NOT change the player's variable. The only way it will, is if the one that works is destroyed.

Here's the script:

 #pragma strict
 
 function Start()
 {
 }
 
 function Update () {
 
 CheckForMob();
 
 }
 
 
 function CheckForMob()
 {
 
 var targets: GameObject[];
 targets = gameObject.FindGameObjectsWithTag("Enemy");
 
 for(var primarytarget in targets)
 {
 
 
 var primtargetdistance : float = Vector3.Distance(transform.position, primarytarget.transform.position); 
 
 
 if(primtargetdistance <= 5)
 {
 
 transform.GetComponent(meleecombat).target = primarytarget.transform;
 
 }
 else
 {
 
 transform.GetComponent(meleecombat).target = null;
 
 }
 
 }
 }


Here's the 'meleecombat' script that this one refers to:

 #pragma strict
 
 var target: Transform;
 
 
 function Update () {
 
 
 
 if(Input.GetKeyDown(KeyCode.Mouse0))
 {
 if(target == null)
 {
 print("You have no target!");
 }
 else
 {
 Attack();
 }
 }
 
 
 
 }
 
 
 function Attack()
 {
 
 var distance: float = Vector3.Distance(transform.position, target.transform.position);
 
 var dir : Vector3 = (target.transform.position - transform.position).normalized;
 var direction: float = Vector3.Dot(dir,transform.forward);
 var hitnum = Random.value;
 
 
 
 
 if(distance < 2.5 && direction > 0.70)
 {
 
 
 if(hitnum >= 0.19)
 {
 print("You attacked the target!");
 target.GetComponent(enemy).AdjustHealth(-5);
 var currhealth : float = target.GetComponent(enemy).currhealth;
 DeathCheck(currhealth);
 }
 else
 {
 
 print("You missed the target!");
 
 }
 
 }
 
 
 
 }
 function DeathCheck(number:float)
 {
 
 if(number == 0)
 {
 Destroy(target.gameObject);
 target = null;
 
 }
 
 }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by jonassejr · Aug 26, 2013 at 02:10 PM

your code only works for one enemy, because you continue checking for enemies after you found one, if the last enemy you are checking is to far away you set the target to null regardless of all the previous enemies.

i rewrote your CheckForMob function so it loops through all the enemies and stops when it finds an enemy close enough.

 function CheckForMob()
 {
    var targets: GameObject[];
    var target: GameObject;

    targets = gameObject.FindGameObjectsWithTag("Enemy");
    for(var primarytarget in targets)
    {
       var primtargetdistance : float = Vector3.Distance(transform.position, primarytarget.transform.position); 
  
       if(primtargetdistance <= 5)
       {
          target = primarytarget.transform;
          break;
       }
    }

    transform.GetComponent(meleecombat).target = target;
 }
Comment
Add comment · Show 2 · 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 baltraven · Aug 26, 2013 at 02:15 PM 0
Share

Thanks alot, I'll look through it, and do the changes, and I'll let you know if it works.

Thanks again

avatar image baltraven · Aug 26, 2013 at 02:29 PM 0
Share

Thanks for your help jonassejr!

All is well, and everything works as expected and as intended.

Good day

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

16 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

Related Questions

Auto Targeting script issue 1 Answer

FindClosestEnemy() change target? 1 Answer

target and look at random enemies 1 Answer

make a ball hit a target 0 Answers

Object Look At Mouse 2 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