Example #1
0
    void fillInRangeList()
    {
        enemyInRange.Clear();
        enemysPlateInRange.Clear();
        platesInRange.Clear();

        float range = attackRange();

        foreach (GameObject elem in enemyCounterList)
        {
            if (FunctionHelperScript.vectorLength(transform.position, elem.transform.position) <= range)
            {
                enemyInRange.Add(elem);
                enemysPlateInRange.Add(elem.GetComponent <CounterMovementScript>().getPlate());
            }
        }

        foreach (GameObject elem in platesList)
        {
            if (FunctionHelperScript.vectorLength(transform.position, elem.transform.position) <= range)
            {
                platesInRange.Add(elem);
            }
        }
    }
    float fireArrow(GameObject plateToAttack)
    {
        Vector3 forceVersor = FunctionHelperScript.makeVersor(transform.position, plateToAttack.transform.position);

        forceVersor = new Vector3(forceVersor.x, forceVersor.y + 1, forceVersor.z);
        forceVersor = forceVersor / FunctionHelperScript.vectorLength(forceVersor);

        float forceToAdd;

        if (onBuilding)
        {
            float bHeight;
            if (plateType == 2)
            {
                bHeight = 0.8f;
            }
            else
            {
                bHeight = 0.53f;
            }
            float length = FunctionHelperScript.vectorLength(new Vector3(transform.position.x, 0, transform.position.z), plateToAttack.transform.position);
            forceToAdd = (9.81f * Mathf.Pow(length, 2)) / (length + bHeight);
            forceToAdd = Mathf.Sqrt(forceToAdd);
        }
        else
        {
            forceToAdd = 3.132f * Mathf.Sqrt(FunctionHelperScript.vectorLength(transform.position, plateToAttack.transform.position));
        }


        GameObject arrowCopy = Instantiate(arrow, transform.position, Quaternion.identity);
        Rigidbody  arrowRb   = arrowCopy.GetComponent <Rigidbody>();

        arrowRb.AddForce(forceVersor * forceToAdd * 10);
        float timeToDestroyArrow = (2 * forceToAdd) / 9.81f;

        Destroy(arrowCopy, timeToDestroyArrow - 0.1f);
        return(timeToDestroyArrow);
    }