Пример #1
0
    public bool RayShoot(bool first)
    {
        bool res = false;

        RaycastHit[]   hits;
        List <casthit> castHits = new List <casthit> ();

        // calculate ray length by running magnitude.
        float   raySize   = runningmMagnitude;
        Vector3 direction = rigidBodyComp.velocity.normalized;

        if (first)
        {
            raySize   = FirstRaylength;
            direction = initialDirection;
        }

        if (raySize <= 2.0f)
        {
            raySize = 2.0f;
        }

        Vector3 pos1 = this.transform.position - (direction * raySize);

        //Vector3 pos2 = pos1 + (direction * raySize);

        if (first)
        {
            pos1 = initialPosition;
            //pos2 = pos1 + (direction * raySize);
        }

        // if you add line renderer. you can see how the ray is casting
        if (lineRenderer)
        {
            LineRenderer line = (LineRenderer)GameObject.Instantiate(lineRenderer, pos1, Quaternion.identity);
            line.SetPosition(0, pos1);
            line.SetPosition(1, pos1 + (raySize * direction));
            line.name = "laser " + raySize + " direction " + direction;
            GameObject.Destroy(line, 10);
        }


        // shoot ray to casting all objects
        int castcount = 0;

        RaycastHit[] casterhits = Physics.RaycastAll(pos1, direction, raySize, ignoreWalkThru);
        for (int i = 0; i < casterhits.Length; i++)
        {
            if (casterhits [i].collider && Vector3.Dot((casterhits [i].point - initialPosition).normalized, initialDirection) > 0.5f)
            {
                // checking a target tags. make sure it hit only taged object.
                if (tagCheck(casterhits [i].collider.tag) && casterhits [i].collider.gameObject != this.gameObject)
                {
                    // checking for make sure it not hit the same object 2 time.
                    if (hitedCheck(casterhits [i].collider))
                    {
                        castcount++;
                        casthit casted = new casthit();
                        casted.distance = Vector3.Distance(initialPosition, casterhits [i].point);
                        casted.index    = i;
                        casted.name     = casterhits [i].collider.name;
                        castHits.Add(casted);
                    }
                }
            }
        }

        // and sorted first to the last by distance
        hits = new RaycastHit[castcount];
        castHits.Sort((x, y) => x.distance.CompareTo(y.distance));

        for (int i = 0; i < castHits.Count; i++)
        {
            hits [i] = casterhits [castHits [i].index];
        }

        for (var i = 0; i < hits.Length && hitcount < HitCountMax; i++)
        {
            RaycastHit hit = hits [i];

            if (first)
            {
                firsthited = true;
            }
            else
            {
                hited = true;
            }

            targetLocked = null;

            Rigidbody hitrig = hit.collider.GetComponent <Rigidbody> ();
            if (hitrig)
            {
                hitrig.AddForce(direction * HitForce * Time.deltaTime, ForceMode.Force);
            }

            // get bullet hiter component.
            AS_BulletHiter bulletHit = hit.collider.gameObject.GetComponent <AS_BulletHiter> ();

            if (bulletHit != null)
            {
                // using action preset
                if (actionPreset && !firsthited && bulletHit.HasAction)
                {
                    actionPreset.BaseDistance = bulletHit.BaseActionDistance;
                    actionPreset.TargetHited(this, bulletHit, hit.point);
                }
                // get particle life time from hiter
                bulletHit.OnHit(hit, this);
            }

            // call do damage function on an object that's got hit.
            hit.collider.SendMessageUpwards(DamageMethodName, (float)Damage, SendMessageOptions.DontRequireReceiver);
            this.SendMessageUpwards(DoHitMethodName, SendMessageOptions.DontRequireReceiver);
            // check a hit number if bullet is still hit under max count.
            res = true;
            hitcount++;
            if (DestroyWhenHit || hitcount >= HitCountMax || tagDestroyerCheck(hit.collider.tag))
            {
                destroyed = true;
            }
        }
        if (destroyed)
        {
            if (actionPreset)
            {
                // using action preset
                actionPreset.OnBulletDestroy();
            }
            // completely removed
            GameObject.Destroy(this.gameObject, DestroyDuration);
        }

        return(res);
    }
Пример #2
0
    public bool RayShoot(bool first)
    {
        bool res = false;

        RaycastHit[]   hits;
        List <casthit> castHits = new List <casthit>();

        float   raySize   = runningmMagnitude;
        Vector3 direction = GetComponent <Rigidbody>().velocity.normalized;

        if (first)
        {
            raySize   = FirstRaylength;
            direction = initialDirection;
        }

        if (raySize <= 2.0f)
        {
            raySize = 2.0f;
        }

        Vector3 pos1 = this.transform.position - (direction * raySize);

        //Vector3 pos2 = pos1 + (direction * raySize);

        if (first)
        {
            pos1 = initialPosition;
            //pos2 = pos1 + (direction * raySize);
        }

        if (lineRenderer)
        {
            LineRenderer line = (LineRenderer)GameObject.Instantiate(lineRenderer, pos1, Quaternion.identity);
            line.SetPosition(0, pos1);
            line.SetPosition(1, pos1 + (raySize * direction));
            line.name = "laser " + raySize + " direction " + direction;
            GameObject.Destroy(line, 10);
            //Debug.Log ("Shoot with size " + raySize);
        }


        // shoot ray to cast all objects
        int castcount = 0;

        RaycastHit[] casterhits = Physics.RaycastAll(pos1, direction, raySize, ignoreWalkThru);
        for (int i = 0; i < casterhits.Length; i++)
        {
            if (casterhits[i].collider && Vector3.Dot((casterhits[i].point - initialPosition).normalized, initialDirection) > 0.5f)
            {
                if (tagCheck(casterhits[i].collider.tag) && casterhits[i].collider.tag != this.gameObject.tag)
                {
                    if (hitedCheck(casterhits[i].collider))
                    {
                        castcount++;
                        casthit casted = new casthit();
                        casted.distance = Vector3.Distance(initialPosition, casterhits[i].point);
                        casted.index    = i;
                        casted.name     = casterhits[i].collider.name;
                        castHits.Add(casted);
                        //Debug.Log("cast "+casterhits[i].collider.name +"  ("+Vector3.Distance(initialPosition,casterhits[i].point)+")");
                    }
                }
            }
            //Debug.Log ("all cast " + casterhits [i].collider.name);
        }

        // sorted first to the last
        hits = new RaycastHit[castcount];
        castHits.Sort((x, y) => x.distance.CompareTo(y.distance));

        for (int i = 0; i < castHits.Count; i++)
        {
            hits[i] = casterhits[castHits[i].index];
            //Debug.Log("soted cast "+castHits[i].index+" to "+i+" "+castHits[i].name+"  ("+castHits[i].distance+")");
        }

        for (var i = 0; i < hits.Length && hitcount < HitCountMax; i++)
        {
            RaycastHit hit = hits[i];

            if (first)
            {
                firsthited = true;
            }
            else
            {
                hited = true;
            }

            GameObject hitparticle = null;

            if (hit.collider.GetComponent <Rigidbody>())
            {
                hit.collider.GetComponent <Rigidbody>().AddForce(direction * HitForce, ForceMode.Force);
            }

            if (hit.collider.gameObject.GetComponent <AS_BulletHiter>())
            {
                AS_BulletHiter bulletHit = hit.collider.gameObject.GetComponent <AS_BulletHiter>();

                if (bulletHit != null)
                {
                    if (bulletHit.ParticleHit)
                    {
                        hitparticle = (GameObject)Instantiate(bulletHit.ParticleHit, hit.point, hit.transform.rotation);
                    }
                    if (actionPreset && !firsthited)
                    {
                        actionPreset.TargetHited(this, bulletHit, hit.point);
                    }

                    this.transform.position = hit.point;

                    bulletHit.OnHit(hit, this);
                    hit.collider.SendMessageUpwards(DamageMethodName, (float)Damage, SendMessageOptions.DontRequireReceiver);
                    this.SendMessageUpwards(DoHitMethodName, SendMessageOptions.DontRequireReceiver);
                }
            }
            else
            {
                if (ParticleHit)
                {
                    hitparticle = (GameObject)Instantiate(ParticleHit, hit.point, hit.transform.rotation);
                }
            }

            if (hitparticle != null)
            {
                hitparticle.transform.forward = hit.normal;
                if (ParticleSticky)
                {
                    hitparticle.transform.parent = hit.collider.transform;
                }
                GameObject.Destroy(hitparticle, 7);
            }

            res = true;
            hitcount++;
            if (DestroyWhenHit || hitcount >= HitCountMax || tagDestroyerCheck(hit.collider.tag))
            {
                destroyed = true;
            }
        }
        if (destroyed)
        {
            if (actionPreset)
            {
                actionPreset.OnBulletDestroy();
            }
            GameObject.Destroy(this.gameObject, 3);
        }

        return(res);
    }