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 rasheedqw · Sep 19, 2013 at 09:54 PM · javascriptcollisionenemydamagehealth

How i make the player damage the enemy specific enemy he is colliding with


        public var target:Transform;
 
        public var attackTimer:float;
 
        public var cooldown:float;
 
        private var anim:Animator ;
 
        public var delay:float=0.1;
    
 function Start () {
 
        attackTimer = 0;
 
        cooldown = 2.0f;
 
        //starts the animator
        anim = GetComponent(Animator);
 
        yield WaitForSeconds(0.8);
 
        Destroy(this); 
   
        }
 
 function Update () {
   
        anim.SetBool("Attack001",false);
 
        go = GameObject.FindGameObjectWithTag("Enemy");
 
        target = go.transform;
 
        if(Input.GetKeyUp(KeyCode.F)){
 
        anim.SetBool("Attack001",true);
        }
        }
  //take 10 from healthbar       
 private function Attack() {
        
        (anim.SetBool("Attack001",true));
 
        var Healthbarph :Healthbar ;
 
        Healthbarph = target.GetComponent("Healthbar");
 
        Healthbarph.AddjustCurrentHealth(-10);
        }
 function OnCollisionEnter(other :Collision){
        if (other.collider.tag == "Enemy"){
   
        Attack();
 
        yield WaitForSeconds(delay);
 
        Destroy(this);
 
        }
        }
   

  


my problem is that this script allows the player to damage the enemy he is colliding with by gathering all the enemies with tag enemy and damage them upon collision but it gathers all the healthbar in a random order so if i collided with enemy a enemy b will take damage what i want is a way to damage the specific enemy i am colliding with i tried to take out all of the targeting lines

 go = GameObject.FindGameObjectWithTag("Enemy");
 
 target = go.transform;

and change

  Healthbarph = target.GetComponent("Healthbar");
 
  Healthbarph.AddjustCurrentHealth(-10);

to

  Healthbarph = GetComponent("Healthbar");
 
  Healthbarph.AddjustCurrentHealth(-10);

but it didn't work

i have a targeting script but cant use it for the as i only targets one person at a time and i need some attacks damage multiply people i am colliding with

here is the healthbar script

       public var maxHealth: int = 100;
 
       public var curHealth: int = 100;
   
       public var healthBarLength: float;
  
 function Start() {
 
       healthBarLength = Screen.width / 2;
 
       }
  
 function Update() {
 
       AddjustCurrentHealth(0);
  
       }
  
 function OnGUI() {
       GUI.Box(Rect(10, 50, healthBarLength/2, 20), curHealth + "/" + maxHealth);
 
       }
  
 function AddjustCurrentHealth(adj: int) {
 
       curHealth += adj;
 
       if(curHealth < 0)
 
       curHealth = 0 ;
 
       if(curHealth > maxHealth)
 
       curHealth = maxHealth;
 
       if(maxHealth < 1)
 
       maxHealth = 1 ;
 
       if(curHealth == 0 ){
 
       Destroy (gameObject, 1);
    
       animation.Play("Death");}
 
       var maxHealthF: float = maxHealth;
 
       healthBarLength = (Screen.width / 2) * (curHealth / maxHealthF);
 
       } 


  
 
Comment
Add comment · Show 9
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 getyour411 · Sep 19, 2013 at 10:14 PM 0
Share

That's a lot of text without a real specific question but it sounds like you need to rethink how you are applying damage. Search for 'find closest enemy', maybe that will work for you.

avatar image rasheedqw · Sep 19, 2013 at 11:59 PM 0
Share

Find closest enemy will just target one enemy plus if the player wants to attack a far away person. It wont let them my question is when I collide with an enemy how to call the attack function to that specific enemy I'm colliding with

avatar image getyour411 · Sep 20, 2013 at 02:56 AM 0
Share

Your script as posted is already doing this: "how to call the attack function to that specific enemy" (re: OnCollisionEnter)

The problem, I think, is that nothing prevents it from calling Attack() for all collisions with your current code they could happen one after another nearly simultaneously. It's not random, it's the order of the collisions.

Add a boolean to OnCollision such that it activates once for the first enemy hit and does your Attack(), but no other Attack()/collision calls will until your Attack() is done and you reset. Note that depending on your game mechanics you might need to add an OnCollisionStay.

Also, find closest enemy could be combined with List/Array to deter$$anonymous$$e closest enemies (plural) and with a distance check to enable ranged. Obviously I don't know your game or what you are trying to accomplish, just added it as food for thought.

avatar image rasheedqw · Sep 20, 2013 at 06:17 AM 0
Share

Ill try to explain it better its a third person game this script is design to call the function. Attack when the player collides with an enemy tagged enemy which it does perfectly. How. I control it is turning the script off and on from another script the problem is it gathers up all the enemies tagged enemy and lets the player attack in a random order what I mean by this is if the player attacks. Enemy A the healthbar for enemy B will go down this is because the srcipt just gathers all enemies healthbar and allow the player to attack a random one upon collision not the specific healthbar of the enemy who the player attacked what I want is the player to damage the specific healthbar of enemy they attacked I would use attack closest enemy but i already explain why it wouldn't work in the comment above thank you for the help I hope this clears things up

avatar image getyour411 · Sep 20, 2013 at 06:55 AM 0
Share

Like I said in my first comment, you should rethink how you are applying damage. For example, your Update() contains

  go = GameObject.FindGameObjectWithTag("Enemy");
  target = go.transform;

and you really don't want to be doing that every Update(). If you have multiple "Enemy" gameobject/tags, exactly what are you expecting target=go.transform to be after every Update()?

Your Attack() function could be changed to receive the collider.transform ins$$anonymous$$d of the generic 'target' but the whole thing seems like it needs some more thought. Good luck!

Show more comments

1 Reply

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

Answer by robertbu · Sep 20, 2013 at 08:15 AM

You say, "gathering all the enemies with tag enemy and damage them upon collision", but how I read you code is that it damages one enemy at "random." That is you call GameObject.FindGameObjectWithTag("Enemy"); which returns some enemy, not necessarily the one you hit. Get rid of target completely, and change the rest of your code as follows:

 private function Attack(go : GameObject) {
  
        (anim.SetBool("Attack001",true));
  
        var Healthbarph :Healthbar ;
  
        Healthbarph = go.GetComponent(Healthbar);
  
        Healthbarph.AddjustCurrentHealth(-10);
        }
 
 
 function OnCollisionEnter(other :Collision){
        if (other.collider.tag == "Enemy"){
  
        Attack(other.collider.gameObject);
  
        yield WaitForSeconds(delay);
  
        Destroy(this);
  
        }
        }

These changes have the game object you collided with used in the Attack() function. Also it is a better practice to use the type rather than a string in GetComponent().

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 rasheedqw · Sep 20, 2013 at 09:14 AM 0
Share

Thank you so much this solved my problem perfectly

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

15 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

Related Questions

How do I destroy a game object (The enemy Goblin Game Object) upon entering a collision box? (JavaScript) 2 Answers

(C#) Enemy health and take damage from bullets 1 Answer

One enemy triggers all the enemies 2 Answers

For loop on collision (two hit kill) 2 Answers

How to cause damage on collision? 1 Answer


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