Exemple #1
0
    private void OnTriggerEnter2D(Collider2D hitInfo)
    {
        BasicEnemyController enemy = hitInfo.GetComponentInParent <BasicEnemyController>();


        if (enemy != null)
        {
            attackDetails[0] = 1f;
            attackDetails[1] = transform.position.x;
            enemy.Damage(attackDetails);
        }

        Debug.Log(hitInfo.name);
        Destroy(gameObject);
    }
Exemple #2
0
    void Update()
    {
        //Check if any enemies in range
        if (targets.Count != 0) //Active state
        {
            var        distance = float.MaxValue;
            GameObject target   = null;

            //Remove any dead enemies
            targets = targets.Where(item => item != null).ToList();
            //Loop through possible targets and find the closest
            foreach (var i in targets)
            {
                var diff        = i.transform.position - transform.position;
                var curDistance = diff.sqrMagnitude;
                if (curDistance < distance)
                {
                    target   = i;
                    distance = curDistance;
                }
            }

            if (target == null)
            {
                return;
            }

            //Rotate to face target
            var lookPos = target.transform.position - transform.position;
            lookPos.y = 0;
            var endRot = Quaternion.LookRotation(lookPos);
            var newRot = Quaternion.RotateTowards(transform.rotation, endRot, Time.deltaTime * rotationSpeed);
            transform.rotation = newRot;

            //Fire every rateOfFire seconds, but only if enemy is in our sights
            timer += Time.deltaTime;
            if (Vector3.Angle(transform.forward, lookPos) < maxFireAngle && timer > rateOfFire)
            {
                //Shoot out our raycast and apply damage to enemy
                StartCoroutine(ShotEffect());
                RaycastHit hit;
                shotLine.SetPosition(0, transform.InverseTransformPoint(barrelExit.position));
                if (Physics.Raycast(barrelExit.position, transform.forward, out hit, range))
                {
                    shotLine.SetPosition(1, transform.InverseTransformPoint(hit.point));
                    BasicEnemyController health = hit.collider.GetComponent <BasicEnemyController>();
                    //Debug.Log("hit " + hit.collider.gameObject.name);
                    if (health != null)
                    {
                        health.Damage(damage, gameObject);
                    }
                }
                else
                {
                    shotLine.SetPosition(1, transform.InverseTransformPoint(transform.forward * range));
                }
                timer = 0f;
            }
        }
        else   //Idle state
        {
            //transform.Rotate(0f, 0.1f, 0f, Space.Self);
        }
    }