示例#1
0
    void HitTarget()
    {
        if (!hit &&
            arrowRayHit.collider != FPSPlayerComponent.FPSWalkerComponent.capsule &&
            !arrowRayHit.collider.gameObject.GetComponent <ArrowObject>())
        {
            hitCol = arrowRayHit.collider;
            myRigidbody.isKinematic  = true;
            transform.gameObject.tag = "Usable";            //allow arroe to be picked up
            //increase collider size to make is easier to retrieve later
            myBoxCol.size = new Vector3(initialColSize.x * 2f, initialColSize.y * 2f, initialColSize.z);
            AmmoPickupComponent.enabled = true;
            transform.position          = arrowRayHit.point;

            if (hitCol.GetComponent <Rigidbody>() || (hitCol.transform.parent != null && hitCol.transform.parent.GetComponent <Rigidbody>()))          //or other moving objects?
            {
                if (hitCol.GetComponent <Rigidbody>())
                {
                    hitCol.GetComponent <Rigidbody>().AddForce(transform.forward * force, ForceMode.Impulse);
                }
                else if (hitCol.transform.parent != null && hitCol.transform.parent.GetComponent <Rigidbody>())                //do additional check for rigidbody on parent object if one not found on hit collider
                {
                    hitCol.transform.parent.GetComponent <Rigidbody>().AddForce(transform.forward * force, ForceMode.Impulse);
                }
                //Create empty parent object for arrow object to prevent arrow from inheriting scale of hit collider if it is skewed or uneven
                emptyObject = new GameObject();
                emptyObject.transform.position = arrowRayHit.point;
                emptyObject.transform.rotation = hitCol.transform.rotation;
                emptyObject.transform.parent   = hitCol.transform;
                transform.parent = emptyObject.transform;
                //empty obj will be destroyed after waitDuration, but obj pool might reuse the prefab and leave the parent obj stranded
                Destroy(emptyObject.gameObject, waitDuration + 1f);
            }

            switch (hitCol.gameObject.layer) //apply damage to hit object
            {
            case 0:                          //hit object
                if (hitCol.gameObject.GetComponent <AppleFall>())
                {
                    hitCol.gameObject.GetComponent <AppleFall>().ApplyDamage(damage + damageAddAmt);
                    FPSPlayerComponent.UpdateHitTime();                    //used for hitmarker
                }
                else if (hitCol.gameObject.GetComponent <BreakableObject>())
                {
                    hitCol.gameObject.GetComponent <BreakableObject>().ApplyDamage(damage + damageAddAmt);
                    FPSPlayerComponent.UpdateHitTime();                    //used for hitmarker
                }
                else if (hitCol.gameObject.GetComponent <ExplosiveObject>())
                {
                    hitCol.gameObject.GetComponent <ExplosiveObject>().ApplyDamage(damage + damageAddAmt);
                    FPSPlayerComponent.UpdateHitTime();                    //used for hitmarker
                }
                else if (hitCol.gameObject.GetComponent <MineExplosion>())
                {
                    hitCol.gameObject.GetComponent <MineExplosion>().ApplyDamage(damage + damageAddAmt);
                    FPSPlayerComponent.UpdateHitTime();                    //used for hitmarker
                }
                break;

            case 1:            //hit object is an object with transparent effects like a window
                if (hitCol.gameObject.GetComponent <BreakableObject>())
                {
                    hitCol.gameObject.GetComponent <BreakableObject>().ApplyDamage(damage + damageAddAmt);
                    FPSPlayerComponent.UpdateHitTime();                    //used for hitmarker
                }
                break;

            case 13:            //hit object is an NPC
                if (hitCol.gameObject.GetComponent <CharacterDamage>() && hitCol.gameObject.GetComponent <AI>().enabled)
                {
                    hitCol.gameObject.GetComponent <CharacterDamage>().ApplyDamage(damage + damageAddAmt, transform.forward, Camera.main.transform.position, transform, true, false);
                    FPSPlayerComponent.UpdateHitTime();                    //used for hitmarker
                }
                if (hitCol.gameObject.GetComponent <LocationDamage>() && hitCol.gameObject.GetComponent <LocationDamage>().AIComponent.enabled)
                {
                    hitCol.gameObject.GetComponent <LocationDamage>().ApplyDamage(damage + damageAddAmt, transform.forward, Camera.main.transform.position, transform, true, false);
                    FPSPlayerComponent.UpdateHitTime();                    //used for hitmarker
                }
                //move arrow more towards center of collider to compensate for body part colliders that are slightly larger than character mesh (prevents floating, stuck arrows)
                transform.position = hitCol.transform.position - ((hitCol.transform.position - arrowRayHit.point).normalized * 0.15f);
                break;

            default:
                break;
            }
            //draw impact effects where the weapon hit
            FPSPlayerComponent.WeaponEffectsComponent.ImpactEffects(hitCol, arrowRayHit.point, false, true, arrowRayHit.normal);

            hitTime = Time.time;
            hit     = true;
        }
    }