예제 #1
0
    void CheckAngleToTarget()
    {
        if (Input.GetMouseButtonUp(1))
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            float      angleBetweenUnits = 0f;
            if (Physics.Raycast(ray, out hit))
            {
                angleBetweenUnits = Vector3.Angle(hit.transform.position - this.transform.position, transform.forward);
            }
            int degree = Mathf.RoundToInt(angleBetweenUnits);
            if (degree < unitMaxFovAngle)
            {
                Debug.Log($"{degree} - possible");
                CheckDistanceToTarget(hit);
            }
            else
            {
                Debug.Log($"{degree} - not possible");
            }
        }

        void CheckDistanceToTarget(RaycastHit hit)
        {
            Vector3   unitPosition   = this.transform.position;
            Vector3   targetPosition = hit.transform.position;
            Transform target         = hit.transform;

            attackingUnit = tgs.CellGetIndex(tgs.CellGetAtPosition(unitPosition, true));
            defendingUnit = tgs.CellGetIndex(tgs.CellGetAtPosition(targetPosition, true));

            int distanceToTarget = tgs.CellGetHexagonDistance(attackingUnit, defendingUnit);

            if (distanceToTarget <= maxRange)
            {
                RangedAttack(target);
            }
            else
            {
                Debug.Log("Target too far away");
            }
        }

        void RangedAttack(Transform target)
        {
            Debug.Log("Attacking---");
            target.GetComponent <UnitHealth>().Damage();
        }
    }