コード例 #1
0
    IEnumerator AnimateProjectile(float arcModifier, float timeModifier)
    {
        bool  hasHit = false;
        float timer  = 0.0f;

        while (!hasHit && timer < 1)
        {
            timer += Time.deltaTime * projectileSpeed * timeModifier;
            Vector3 newEndPos = Vector3.Lerp(
                EndPosition,
                EndPosition + Vector3.up * arcModifier,
                Mathf.Sin(Mathf.Lerp(0, Mathf.PI, timer)));
            Vector3 curPos = Vector3.Lerp(StartPosition, newEndPos, timer);
            transform.LookAt(curPos);
            transform.position = curPos;

            RaycastHit hit;
            float      distance = 3f;                                           //Why 3?
            Vector3    dir      = Vector3.Normalize(EndPosition - transform.position);
            Debug.DrawRay(curPos, dir, Color.red);
            if (Physics.Raycast(curPos, dir, out hit, distance, attackMask))
            {
                if (!hit.transform.CompareTag(transform.tag))
                {
                    Base_NW enemy = hit.transform.root.GetComponent <Base_NW>();

                    GameObject impact = EffectRecycler.effectRecycler.GetEffect(MyWeapon.hit);
                    impact.transform.position = hit.point;
                    impact.SetActive(true);

                    if (enemy != null)
                    {
                        enemy.TakeDamage(MyWeapon.damage);
                    }

                    hasHit = true;

                    if (projectileObject)
                    {
                        projectileObject.SetActive(false);
                    }

                    StartCoroutine(HideEffect());
                }
            }
            else if (Target == null)
            {
                if (projectileObject)
                {
                    projectileObject.SetActive(false);
                }
                StartCoroutine(HideEffect());
            }
            yield return(null);
        }
    }
コード例 #2
0
ファイル: Unit_NW.cs プロジェクト: w1shmast3r/STW2017.1.0-RTS
    IEnumerator TrackTarget()
    {
        Base_NW enemy = null;

        while (target != null && target.gameObject.activeSelf && muzzle.Length > 0 && !IsDead)
        {
            for (int j = 0; j < muzzle.Length; j++)
            {
                Vector3 forward   = Vector3.Normalize(muzzle[j].TransformDirection(Vector3.forward));
                Vector3 targetDir = Vector3.zero;

                if (target)
                {
                    targetDir = Vector3.Normalize(target.position - muzzle[j].position);
                }

                float facing = Vector3.Dot(forward, targetDir);

                //if turret is not facing towards target, don't fire
                if (facing < 0.95f)
                {
                    break;
                }


                // turret.LookAt(target.transform);
                Vector3      offset    = Vector3.up * 0.5f;
                Vector3      direction = Vector3.Normalize((target.position + offset) - muzzle[j].transform.position);
                RaycastHit[] impacts   = Physics.RaycastAll(muzzle[j].position, direction, weapon.range, attackMask);


                //scrap target if targeting own team
                if (target.CompareTag(transform.tag))
                {
                    break;
                }

                if (impacts.Length > 0)
                {
                    for (int i = 0; i < impacts.Length; i++)
                    {
                        if (impacts[i].transform.root != transform.root)
                        {
                            GameObject muzzleFlash = EffectRecycler.effectRecycler.GetEffect(weapon.muzzleFlash);
                            muzzleFlash.transform.parent        = muzzle[j];
                            muzzleFlash.transform.localPosition = Vector3.zero;
                            muzzleFlash.transform.localRotation = Quaternion.identity;
                            muzzleFlash.transform.localScale    = Vector3.one;
                            muzzleFlash.SetActive(true);


                            if (weapon.hasProjectile)
                            {
                                GameObject       projectile       = EffectRecycler.effectRecycler.GetEffect(weapon.projectileType);
                                ProjectileWeapon projectileScript = projectile.GetComponent <ProjectileWeapon>();
                                projectileScript.StartPosition = muzzle[j].position;
                                projectileScript.EndPosition   = impacts[i].point;
                                projectileScript.MyWeapon      = weapon;
                                projectileScript.Target        = impacts[i].transform.root.gameObject;
                                projectile.tag = transform.tag;
                                projectile.SetActive(true);
                            }
                            else
                            {
                                //cache enemy base class
                                if (enemy == null)
                                {
                                    enemy = impacts[i].transform.root.GetComponent <Base_NW>();
                                }

                                GameObject hit = EffectRecycler.effectRecycler.GetEffect(weapon.hit);
                                hit.transform.position = impacts[i].point;
                                hit.SetActive(true);

                                if (enemy != null && enemy.TakeDamage(weapon.damage))
                                {
                                    target = null;
                                }
                                break;
                            }
                        }
                    }
                }

                yield return(new WaitForSeconds(0.1f));
            }
            yield return(new WaitForSeconds(1 / weapon.rateOfFire));
        }

        yield return(new WaitForEndOfFrame()); //wait for a frame to avoid

        StartCoroutine(FindTarget());
    }