• 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 kost · Feb 23, 2012 at 11:18 PM · projectile

need help with projectile taking enemy health c# script

I need help with projectile taking enemy health c# script.

Comment
Add comment · Show 3
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 aldonaletto · Feb 23, 2012 at 11:20 PM 0
Share

If you are having problems with your script, edit your question and post it here.

avatar image kost · Feb 24, 2012 at 11:43 AM 0
Share

using UnityEngine; using System.Collections;

public class Projectile : $$anonymous$$onoBehaviour {

 public float ProjectileSpeed;
 public float _curHealth;
 
 
 private Transform myTransform;
 
 void Start ()
 {
    myTransform = transform;
 }
 
 
 void Update ()
 {
    float amtTo$$anonymous$$ove = ProjectileSpeed * Time.deltaTime;
    myTransform.Translate(Vector3.forward * amtTo$$anonymous$$ove);
  
   
         Destroy(gameObject, 2f);
 }
 
 void OnTriggerEnter(Collider otherObject)
 {
     if(otherObject.tag == "Enemy")
     {
       otherObject.gameObject.GetComponent("_curHealth");
         
     //  _curHealth(-10);
         
     //if  ( _curHealth < 0)
     //    {
         Destroy(otherObject.gameObject);
     //    }    
     }    
 }    

}//its all working if I don't take enemyhealth down. How can I connect EnemyHealth script correctly. Any ideas ?

avatar image MD_Reptile · Feb 24, 2012 at 01:11 PM 0
Share

Wouldnt you want this part here to be "if less than 0.1 destory object" ins$$anonymous$$d? Because he wont be less than zero (unless you script it that way to damage below 0...) when he is dead, he could be exactly zero and health bar is depleted...

if (_curHealth < 0.1) { Destroy(otherObject.gameObject); }

and as far as taking down the health, seems easy enough to me, make a float or var or whatever that holds an amount, lets say 100, for your "enemy Health" thats attached to the enemy in a script called something like "entityHealth.cs" and you could just instance that script, and use the script that detects the projectile hit, to change entityHealth's "enemy Health" var/float/whateva...and run checks in one of your scripts attached to that enemy/player that will detect if the health number drops below 0.1 (or whateva) and then if so, stop motor control, and stop camera or whatnot, start ragdoll of said entity, you know just something along those lines.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by devPhil · Feb 25, 2012 at 03:08 AM

You need to access the enemy's script, then access two public functions within that script. One to tell the enemy to take damage and one to get the current health of the enemy.

 void OnTriggerEnter(Collider otherObject)
 {
     if(otherObject.tag == "Enemy")
     {
         // EnemyScript is attached to the enemy
         EnemyScript e = otherObject.gameObject.GetComponent<EnemyScript>();
         
         // Create a public function in the EnemyScript to tell it to take damage and pass the damage
         e.TakeDamage(10f);
     
         // Make a public function in EnemyScript to return the health of the enemy
         if  ( e.GetHealth() <= 0f)
         {
             Destroy(otherObject.gameObject);
         }   
     }  
 }

Here are is the GetHealth() and TakeDamage() functions just in case.

 public float GetHealth()
 {
      return currentHealth;
 }
 
 public void TakeDamage(float damage)
 {
      currentHealth -= damage;
 }

Hope this helps.

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 kost · Mar 01, 2012 at 12:44 PM 0
Share

I use ProjectilePrefab as component of player script. Then I drag down "Enemy" as target which is component of ProjectilePrefab. After I corrected Projectile script it works. But Projectiles take enemy health even if they fly wrong direction. So this thing void OnTriggerEnter doesn't work correctly. Actually can I use components, targets of projectileprefab script ?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Top down shooter trajectory 1 Answer

Projectile in online multiplayer 1 Answer

Problem when creating a projectile for a weapon. (FIXED) 1 Answer

Rotation troubles 2 Answers

Projectile not creating particle effects on hit 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