- Home /
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.
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);
}
}
Your answer
Follow this Question
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