Search Unity

lower health on collision

Discussion in 'Scripting' started by Mr.badatscripting, Oct 26, 2010.

  1. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    Ok I am making a zombie game i have got the enemy AI made but i need to know how to make my person lose health as he gets hit with an animation (zombies arms swinging). Can anyone help?
     
  2. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    Create a trigger volume ( a collider with isTrigger checked ) and attach it to your zombie where the damage zone should be. Make sure it's got a Rigidbody component so it handles physics properly while moving.

    Attach this script to that trigger volume:

    Code (csharp):
    1. var damageDone : int;
    2.  
    3. function OnTriggerEnter( other : Collider ) {
    4.   if ( other.GetComponent( Player ) ) {
    5.     other.GetComponent( Player ).health -= damageDone;
    6.   }
    7. }
    You'll have to replace Player with whatever it is on your player object that handles health, and obviously have a var health in that script somewhere.

    Your Zombie can then keep track of its trigger volume and activate/deactivate it based on whether it wants to deal damage or no.
     
  3. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    thank you very much
     
  4. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Last edited: Oct 26, 2010
  5. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132
    I don't know whats happening i am useing this script to control health but nothing is happening i filled in everything i think.

    #pragma strict
    #pragma implicit
    #pragma downcast

    class DestroyOnHit extends MonoBehaviour
    {
    public var hitsToDestroy : int = 1;
    public var destructionParticles : GameObject;
    public var destroyOnExplosion : boolean = true;
    public var destroyParent : boolean = true;

    function Start()
    {
    gameObject.layer = 11;
    }

    function Destruct()
    {
    if(destroyOnExplosion)
    {
    DestroyObject();
    }
    }

    function Hit(hit : RaycastHit)
    {
    hitsToDestroy--;

    if(hitsToDestroy <= 0)
    {
    DestroyObject();
    }
    }

    function DestroyObject()
    {
    if(destructionParticles != null)
    {
    GameObject.Instantiate(destructionParticles, transform.position, Quaternion.identity);
    }

    if(destroyParent)
    {
    if(transform.parent != null)
    {
    Destroy(transform.parent.gameObject);
    }
    else
    {
    Destroy(gameObject);
    }
    }
    else
    {
    Destroy(gameObject);
    }
    }
    }
     
  6. Mr.badatscripting

    Mr.badatscripting

    Joined:
    Jul 1, 2010
    Posts:
    132