Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ddunteman · Dec 08, 2016 at 06:46 AM · c#unity 5damagezombieshealth-deduction

Urgent help for Unity Game with deadline tomorrow

I am trying to make my tank shells do damage to these zombies, but I do not know how to reconcile the script where my shells do damage with the script where the zombies take damage. The relevant parts of each script are below:

For ZombieInstance.cs, which is a component of the zombie itself:

     public void Damage(float dmg){
         /*HERE YOU CALL DAMAGE FOR ZOMBIE EXAMPLE: 
         *zombieObject.SendMessage("ZombieDamage", float value, SendMessageOptions.RequireReceiver);
         */
         hp -= dmg;
 
         if(player != null)
             chasePlayer();
 
         if(hp < 0.0f || hp == 0.0f){//This zombie is freakin' dead!
             this.tag = "Untagged";
             BroadcastMessage("ActivateR", SendMessageOptions.RequireReceiver);//Activate ragdoll or spawn the dead bodie.
             Anim.enabled = false;//Deactivate things that might screw the ragdoll up.
             agent.enabled = false;
             GetComponent<Collider>().enabled = false;
             Destroy(gameObject, bodieRemovalTime);
             enabled = false;
         }
     }

For ShellExplosion.cs, which is a component of the shell that explodes and deals damage over a radius from the center of the explosion:

         public void OnTriggerEnter (Collider other)
         {
             // Collect all the colliders in a sphere from the shell's current position to a radius of the explosion radius.
             Collider[] colliders = Physics.OverlapSphere (transform.position, m_ExplosionRadius, m_TankMask);
 
             // Go through all the colliders...
             for (int i = 0; i < colliders.Length; i++)
             {
                 // ... and find their rigidbody.
                 Rigidbody targetRigidbody = colliders[i].GetComponent<Rigidbody> ();
 
                 // If they don't have a rigidbody, go on to the next collider.
                 if (!targetRigidbody)
                     continue;
 
                 // Add an explosion force.
                 targetRigidbody.AddExplosionForce (m_ExplosionForce, transform.position, m_ExplosionRadius);
 
                 // Find the TankHealth script associated with the rigidbody.
                 ZombieInstance targetHealth = targetRigidbody.GetComponent<ZombieInstance> ();
 
                 // If there is no TankHealth script attached to the gameobject, go on to the next collider.
                 if (!targetHealth)
                     continue;
 
                 // Calculate the amount of damage the target should take based on it's distance from the shell.
                 float damage = CalculateDamage (targetRigidbody.position);
 
                 // Deal this damage to the tank.
                 targetHealth.Damage (damage);
 
                 targetHealth.SendMessage("Damage", damage, SendMessageOptions.DontRequireReceiver);
             }
 
             // Unparent the particles from the shell.
             m_ExplosionParticles.transform.parent = null;
 
             // Play the particle system.
             m_ExplosionParticles.Play();
 
             // Play the explosion sound effect.
             m_ExplosionAudio.Play();
 
             // Once the particles have finished, destroy the gameobject they are on.
             Destroy (m_ExplosionParticles.gameObject, m_ExplosionParticles.duration);
 
             // Destroy the shell.
             Destroy (gameObject);
         }
 
 
         public float CalculateDamage (Vector3 targetPosition)
         {
             // Create a vector from the shell to the target.
             Vector3 explosionToTarget = targetPosition - transform.position;
 
             // Calculate the distance from the shell to the target.
             float explosionDistance = explosionToTarget.magnitude;
 
             // Calculate the proportion of the maximum distance (the explosionRadius) the target is away.
             float relativeDistance = (m_ExplosionRadius - explosionDistance) / m_ExplosionRadius;
 
             // Calculate damage as this proportion of the maximum possible damage.
             float damage = relativeDistance * m_MaxDamage;
 
             // Make sure that the minimum damage is always 0.
             damage = Mathf.Max (0f, damage);
 
             return damage;
         }

I want the damage calculated in the ShellExplosion to affect the zombie's health. How can I change my code to make this work? Thanks in advance.

Comment
Add comment · Show 1
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 ddunteman · Dec 07, 2016 at 08:46 PM 0
Share

Also, here is the script for how the tank shoots the shells--> It doesn't use Raycast which is why I feel like I need to change the ZombieInstance script, but I don't know how.

 var projectile : Rigidbody;
 var speed = 10;
 var fireRate = 2.0;
 var nextFire = 0.0;
 
 function Update () {
 
     if (Input.GetButtonUp("Fire01") && (Time.time > nextFire))  {
 
         nextFire = Time.time + fireRate;
         var clone = Instantiate (projectile, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection (Vector3 (0, 0, speed));
         Destroy (clone.gameObject, 5);
 
 
     }
 }

0 Replies

· Add your reply
  • Sort: 

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

7 People are following this question.

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

Related Questions

Display Hit Text On Collition 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

UTF8 string acquired through NAMEDPIPES unrecognized by Unityengine 2 Answers

Unity Touch sensitivity issue on Samsung Devices 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