Example #1
0
    public void HitWithArrow(EnemyBodyPart bodyPart, Arrow arrow)
    {
        Health health = GetComponent <Health>();

        if (health.currentHealth <= 0)
        {
            return;
        }
        bool headHit = (bodyPart.gameObject.name == "HeadBone");

        health.TakeDamage(arrow.damage);

        if (hitSound && !hitSound.isPlaying)
        {
            hitSound.pitch = Random.Range(0.8f, 1.2f);
            hitSound.Play();
        }

        if (headHit)
        {
            health.TakeDamage(arrow.damage * 10);
        }

        if (health.currentHealth <= 0)
        {
            //Destroy(this.gameObject);
            foreach (Rigidbody body in GetComponentsInChildren <Rigidbody>())
            {
                body.velocity        = Vector3.zero;
                body.angularVelocity = Vector3.zero;
            }

            bodyController.Ragdoll();
            // If we just died, also make it look like enemy was shot
            if (headHit || bodyPart.gameObject.name == "SpineBone")
            {
                Vector3 hitDirection = Vector3.Normalize(bodyPart.transform.position - arrow.transform.position) * -1;
                // bodyPart.GetComponent<Rigidbody>().velocity = hitDirection * arrow.transform.GetComponent<Rigidbody>().velocity.magnitude * 0.9f;
                bodyPart.GetComponent <Rigidbody>().velocity = hitDirection * 10f;
                Debug.Log("Hit in " + this.name + " for velocity " + bodyPart.GetComponent <Rigidbody>().velocity);
            }
            rightAfterDeathTimer = new Timer();
            rightAfterDeathTimer.Start(1f);

            if (deathSound && !deathSound.isPlaying)
            {
                deathSound.pitch = Random.Range(0.8f, 1.2f);
                deathSound.Play();
            }
        }
    }
Example #2
0
    //public void OnCollisionEnter(Collision other)
    //{
    //    if (other.gameObject.layer == LayerMask.NameToLayer("PlayerLayer"))
    //    {
    //        return;
    //    }

    //    noHitTimer = null;

    //    Debug.Log("Hit: " + other.gameObject.name);

    //    Health otherHealth = other.gameObject.GetComponentInParent<Health>();

    //    if (otherHealth != null)
    //    {
    //        Enemy enemy = other.gameObject.GetComponentInParent<Enemy>();
    //        EnemyBodyPart enemyPart = other.gameObject.GetComponent<EnemyBodyPart>();
    //        if (enemy == null || enemyPart == null)
    //            return;
    //        //if(enemy != null && enemyPart != null)
    //        //{
    //        enemy.HitWithArrow(enemyPart, this);
    //        //}
    //        //else {
    //        //    otherHealth.TakeDamage(damage);
    //        //}

    //    }
    //    else
    //    {
    //        deathTimer = new Timer();
    //        deathTimer.Start(60f);
    //    }

    //    var heading = other.contacts[0].point - this.transform.position;
    //    var distance = heading.magnitude;
    //    var direction = heading / distance; // This is now the normalized direction.

    //    Debug.Log("Distance: " + distance.ToString());

    //    this.transform.position = this.transform.position + direction * distance / 2;

    //    // do this to avoid scaling issues
    //    GameObject newObj = new GameObject();
    //    this.transform.parent = newObj.transform;
    //    newObj.transform.parent = other.transform;
    //    //newObj.transform.parent = other.GetComponentInParent<Enemy>().GetComponentInChildren<EnemyBodyPart>().gameObject.transform;

    //    this.GetComponent<Rigidbody>().velocity = Vector3.zero;
    //    this.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    //    this.GetComponent<Rigidbody>().useGravity = false;
    //    this.GetComponent<Rigidbody>().isKinematic = true;
    //    //this.transform.parent = other.transform;
    //    this.GetComponent<CapsuleCollider>().enabled = false;


    //}

    //public void OnTriggerEnter(Collider other)
    //{
    //    if (other.gameObject.layer == LayerMask.NameToLayer("PlayerLayer"))
    //    {
    //        return;
    //    }

    //    noHitTimer = null;

    //    Debug.Log("Hit: " + other.gameObject.name);

    //    Health otherHealth = other.gameObject.GetComponentInParent<Health>();
    //    Enemy enemy = other.gameObject.GetComponentInParent<Enemy>();
    //    EnemyBodyPart enemyPart = other.gameObject.GetComponent<EnemyBodyPart>();
    //    if (otherHealth != null && enemy != null && enemyPart != null)
    //    {

    //        //if (enemy == null || enemyPart == null)
    //        //    return;
    //        //if(enemy != null && enemyPart != null)
    //        //{
    //        enemy.HitWithArrow(enemyPart, this);
    //        //}
    //        //else {
    //        //    otherHealth.TakeDamage(damage);
    //        //}

    //        //var heading = other.transform.position - this.transform.position;
    //        //var distance = heading.magnitude;
    //        //var direction = heading / distance; // This is now the normalized direction.

    //        //Debug.Log("Distance: " + distance.ToString());

    //        //this.transform.position = this.transform.position + direction * distance * 0.8f;
    //        //this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, other.transform.position.z);
    //    }
    //    else
    //    {
    //        deathTimer = new Timer();
    //        deathTimer.Start(60f);
    //    }

    //    this.transform.position = other.GetComponent<Collider>().ClosestPointOnBounds(this.transform.position);
    //    //var heading = other.transform.position - this.transform.position;
    //    //var distance = heading.magnitude;
    //    //var direction = heading / distance; // This is now the normalized direction.

    //    //Debug.Log("Distance: " + distance.ToString());

    //    //this.transform.position = this.transform.position + direction * distance * 0.95f;
    //    //this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z - 10f);


    //    //Vector3 newPosition1 = new Vector3(this.transform.position.x, this.transform.position.y, other.transform.position.z);
    //    //Vector3 newPosition2 = new Vector3(this.transform.position.x, this.transform.position.y, -other.transform.position.z);

    //    //if(Vector3.Distance(newPosition1, this.transform.position) < Vector3.Distance(newPosition2, this.transform.position))
    //    //{
    //    //    this.transform.position = newPosition1;
    //    //}
    //    //else
    //    //{
    //    //    this.transform.position = newPosition2;
    //    //}



    //    // do this to avoid scaling issues
    //    GameObject newObj = new GameObject();
    //    this.transform.parent = newObj.transform;
    //    newObj.transform.parent = other.transform;


    //    //newObj.transform.parent = other.GetComponentInParent<Enemy>().GetComponentInChildren<EnemyBodyPart>().gameObject.transform;

    //    this.GetComponent<Rigidbody>().velocity = Vector3.zero;
    //    this.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
    //    this.GetComponent<Rigidbody>().useGravity = false;
    //    this.GetComponent<Rigidbody>().isKinematic = true;
    //    //this.transform.parent = other.transform;
    //    this.GetComponent<CapsuleCollider>().enabled = false;


    //}

    public void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.layer == LayerMask.NameToLayer("PlayerLayer"))
        {
            return;
        }

        noHitTimer = null;

        //Debug.Log("Hit: " + other.gameObject.name);

        Health        otherHealth = other.gameObject.GetComponentInParent <Health>();
        Enemy         enemy       = other.gameObject.GetComponentInParent <Enemy>();
        EnemyBodyPart enemyPart   = other.gameObject.GetComponent <EnemyBodyPart>();

        if (otherHealth != null && enemy != null && enemyPart != null)
        {
            //if (enemy == null || enemyPart == null)
            //    return;
            //if(enemy != null && enemyPart != null)
            //{
            enemy.HitWithArrow(enemyPart, this);
            //}
            //else {
            //    otherHealth.TakeDamage(damage);
            //}

            //var heading = other.transform.position - this.transform.position;
            //var distance = heading.magnitude;
            //var direction = heading / distance; // This is now the normalized direction.

            //Debug.Log("Distance: " + distance.ToString());

            //this.transform.position = this.transform.position + direction * distance * 0.8f;
            //this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, other.transform.position.z);
        }
        else
        {
            deathTimer = new Timer();
            deathTimer.Start(60f);
        }


        var heading   = other.contacts[0].point - this.transform.position;
        var distance  = heading.magnitude;
        var direction = heading / distance; // This is now the normalized direction.

        //Debug.Log("Distance: " + distance.ToString());

        this.transform.position = this.transform.position + direction * distance / 2;



        // do this to avoid scaling issues
        GameObject newObj = new GameObject();

        this.transform.parent   = newObj.transform;
        newObj.transform.parent = other.transform;

        AudioPlayer audioPlayer = other.gameObject.GetComponent <AudioPlayer>();

        if (audioPlayer != null)
        {
            audioPlayer.PlaySound();
        }

        //newObj.transform.parent = other.GetComponentInParent<Enemy>().GetComponentInChildren<EnemyBodyPart>().gameObject.transform;

        this.GetComponent <Rigidbody>().velocity        = Vector3.zero;
        this.GetComponent <Rigidbody>().angularVelocity = Vector3.zero;
        this.GetComponent <Rigidbody>().useGravity      = false;
        this.GetComponent <Rigidbody>().isKinematic     = true;
        //this.transform.parent = other.transform;
        this.GetComponent <CapsuleCollider>().enabled = false;
    }