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 Aydan · Apr 25, 2011 at 02:28 AM · playerenemydamageattack

auto select enemys

Hello im trying to make a game that has a fighting system like oblivion. I am trying to make it so any enemy within a certain distance of the player looses health i DON'T want it to be like most mmorpg's where they target enemys i want it to be base on skill. Heres my script for PlayerAttack:

var target : GameObject; var attackTimer : float; var coolDown : float; var damage : float = 10;

// Use this for initialization function Start () { attackTimer = 0; coolDown = 1.0f;

}

// Update is called once per frame function Update () { if(attackTimer > 0) attackTimer -= Time.deltaTime;

 if (attackTimer < 0)
     attackTimer = 0;
 if(Input.GetKeyUp(KeyCode.F)) {
     if(attackTimer == 0){
         Attack();
         attackTimer = coolDown;
     }
 }

} function Attack() { var distance = Vector3.Distance(target.transform.position, transform.position);

 var direction = Vector3.Dot((target.transform.position - transform.position).normalized, transform.forward);

 Debug.Log(direction);

 if(distance < 3) {
     if(direction > 0) {
         target.GetComponent(EnemyHealth).AddjustCurrentHealth(-damage);
     }
 }

}

heres my EnemyHealth script:

var curHealth : float = 20; var maxHealth : float = 20;

function Update () { AddjustCurrentHealth(0);

}

function AddjustCurrentHealth(adj) { curHealth += adj;

 if(curHealth < 0)
     curHealth = 0;

 if (curHealth > maxHealth)
     curHealth = maxHealth;

 if(maxHealth < 1)
     maxHealth = 1;

 if(curHealth < 1) { //die
     Destroy(gameObject);
 }

}

the scripts currently work fine but I want it to be adapted to muliple enemys. I want it to find GameObject.tag == "Enemy"; within a distance of 3 or something and is in the direction im facing or something along those lines. Can someone please help thanks.

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 GesterX · Apr 25, 2011 at 02:46 AM 0
Share

It would be helpful if you described what behaviour your seeing now? Does this currently not work? Or would you want the code adapted for multiple enemies?

avatar image Aydan · Apr 25, 2011 at 03:31 AM 0
Share

ok currently the script works but i dont want to only have one enemy (the target variable) i want to be able to walk up to any enemy and attack it but only that one looses health.

avatar image Aydan · Apr 25, 2011 at 03:32 AM 0
Share

oh and yes i want it to be adapted for multiple enemys thanks

avatar image Aydan · Apr 25, 2011 at 09:00 AM 0
Share

Please i really need this to continue with my game

3 Replies

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

Answer by Casper 1 · Apr 25, 2011 at 10:39 AM

What you want to do is set the tag "Enemy" for all your enemies.

In your PlayerAttack you find all gameobjects with the tag enemy (Save them into an array), then you find the distance between your player and your enemies (Save the distance in the array above as well).

Select the enemy who has the lowest distance and attack him.

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 Aydan · Apr 25, 2011 at 09:44 PM 0
Share

I've got the tag enemy for all my enemy's, but how do I save them as an array and find what one is closest I don't know alot about scripting. Thanks

avatar image Casper 1 · Apr 26, 2011 at 02:00 PM 0
Share

IF the below (Answer by GesterX) doesn't work you can look here http://answers.unity3d.com/questions/54813/how-do-i-get-my-script-to-work - He is trying to accomplish the same as you

avatar image
0
Best Answer

Answer by GesterX · Apr 25, 2011 at 01:24 PM

Look at the second example on this page of the scripting reference here: http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html

It contains an example of a function to return the closest enemy. You can then just use this to find the closest enemy and decrease his health etc...

Comment
Add comment · Show 1 · 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 Aydan · Apr 25, 2011 at 09:45 PM 0
Share

Ok I'll try this thanks.

avatar image
0

Answer by Aydan · Apr 26, 2011 at 09:21 PM

Here I figured it out thanks for your help.

Targeting Script:

var target = closest; var closest : GameObject;

function Update(){ target = closest; // Find all game objects with tag Enemy var enemies : GameObject[]; enemies = GameObject.FindGameObjectsWithTag("Enemy"); var distance = Mathf.Infinity; var position = transform.position; // Iterate through them and find the closest one for (var enemy : GameObject in enemies) { var diff = (enemy.transform.position - transform.position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) { closest = enemy; distance = curDistance; } } return closest; }

PlayerAttack Script

var target : GameObject; var attackTimer : float; var coolDown : float; var damage : float = 10;

// Use this for initialization function Start () { attackTimer = 0; coolDown = 1.0f;

}

// Update is called once per frame function Update () { target = GetComponent(Targeting).target;

 if(attackTimer &gt; 0)
     attackTimer -= Time.deltaTime;

 if (attackTimer &lt; 0)
     attackTimer = 0;
 if(Input.GetKeyUp(KeyCode.F)) {
     if(attackTimer == 0){
         Attack();
         attackTimer = coolDown;
     }
 }

} function Attack() { var distance = Vector3.Distance(target.transform.position, transform.position);

 var direction = Vector3.Dot((target.transform.position - transform.position).normalized, transform.forward);

 Debug.Log(direction);

 if(distance &lt; 3) {
     if(direction &gt; 0) {
         target.GetComponent(EnemyHealth).AddjustCurrentHealth(-damage);
     }
 }

}

Thanks to GesterX and Casper.

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

No one has followed this question yet.

Related Questions

Player attacks once but the enemy takes double damages! 1 Answer

How do I hurt enemies by jumping on them without getting killed? 1 Answer

enemy attack the player 0 Answers

I use this script, but the enemy lose health if i don´t target him. 1 Answer

Player Damage to enemy damage melee system 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