Esempio n. 1
0
        public void Shoot(AttackInstance2D attInst = null, Transform sp = null)
        {
            if (attInst.tgtUnit == null || attInst.tgtUnit.GetTargetTransform() == null)
            {
                ObjectPoolManager.Unspawn(gameObject);
                return;
            }

            attInstance  = attInst;
            target       = attInstance.tgtUnit;
            targetPos    = target.GetTargetTransform().position;
            hitThreshold = Mathf.Max(.1f, target.hitThreshold);
            shootPoint   = sp;

            if (type != _ShootObjectType.Beam)
            {
                if (shootPoint.position.x > targetPos.x)
                {
                    renderer.flipX = true;
                }
                else
                {
                    renderer.flipX = false;
                }
            }

            //if (shootPoint != null) transform.rotation = shootPoint.rotation;
            if (shootEffect != null && target.isAlive)
            {
                ObjectPoolManager.Spawn(shootEffect, transform.position, transform.rotation);
            }
            hit = false;

            print("Shoot");
            if (type == _ShootObjectType.Projectile)
            {
                StartCoroutine(ProjectileRoutine());
            }
            else if (type == _ShootObjectType.Beam)
            {
                StartCoroutine(BeamRoutine());
            }
            else if (type == _ShootObjectType.Bullet)
            {
                StartCoroutine(BulletRoutine());
            }
            else if (type == _ShootObjectType.Missile)
            {
                StartCoroutine(MissileRoutine());
            }
            else if (type == _ShootObjectType.Effect && effectDelay > 0)
            {
                StartCoroutine(EffectRoutine());
            }
            else
            {
                Hit();
            }
        }
Esempio n. 2
0
        private IEnumerator BulletRoutine()
        {
            print("BulletRoutine");

            if (shootEffect != null)
            {
                ObjectPoolManager.Spawn(shootEffect, transform.position, transform.rotation);
            }
            yield return(new WaitForSeconds(shootDelay));

            //while the shootObject havent hit the target
            while (!hit)
            {
                if (target != null)
                {
                    targetPos = target.GetTargetTransform().position;
                }
                //calculating distance to targetPos
                Vector3 curPos = transform.position;
                //curPos.y = y;
                float currentDist = Vector2.Distance(curPos, targetPos);
                //if the target is close enough, trigger a hit
                if (currentDist < hitThreshold && !hit)
                {
                    Hit();
                    break;
                }

                transform.LookAt(targetPos);
                //move forward
                transform.Translate(Vector3.forward * Mathf.Min(speed * Time.deltaTime, currentDist));
                //transform.rotation = Quaternion.Euler(0, 0, 0);
                transform.rotation = Quaternion.Euler(0, 0, -transform.rotation.eulerAngles.x);
                speed += accel * Time.deltaTime;
                yield return(null);
            }
        }