Ejemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        GameObject nearestTank = null;
        float      minDist     = 1000;
        Vector3    currentPos  = transform.position;

        foreach (GameObject tank in tankTeam)
        {
            float dist = Vector3.Distance(tank.transform.position, currentPos);
            if (dist < minDist)
            {
                nearestTank = tank;
                minDist     = dist;
            }
        }

        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        float distance = Vector3.Distance(nearestTank.transform.position, transform.position);

        if (distance > 50)
        {
            far = true;

            foreach (Renderer rend in GetComponentsInChildren <Renderer>())
            {
                rend.material.color = Color.red;
            }
        }
        else
        {
            foreach (Renderer rend in GetComponentsInChildren <Renderer>())
            {
                rend.material.color = Color.white;
            }
            far = false;
        }

        if (Physics.Raycast(ray, out hit))
        {
            Vector3 lastPos = movePos;
            if (hit.collider.tag != "Unit" && hit.collider.tag != "SpawningTank" && hit.collider.tag != "MainCamera" && hit.point.y > 12)
            {
                movePos = hit.point;
                lastPos = movePos;
            }
            else
            {
                movePos = lastPos;
            }
        }

        Vector3 activePos = new Vector3(movePos.x, movePos.y + 2, movePos.z);

        transform.position = activePos;

        if (Input.GetMouseButton(0) && !far)
        {
            spawnTank.SpawnTankAtPos(activePos);
        }

        if (Input.GetMouseButton(1))
        {
            spawnTank.CancelSpawn();
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        if (transform.position.z > boundaryForwardPos.z)
        {
            far = true;
        }
        else if (transform.position.z < boundaryBackPos.z)
        {
            far = true;
        }
        else if (transform.position.x < boundaryLeftPos.x)
        {
            far = true;
        }
        else if (transform.position.x > boundaryRightPos.x)
        {
            far = true;
        }
        else
        {
            far = false;
        }

        if (far)
        {
            foreach (Renderer rend in GetComponentsInChildren <Renderer>())
            {
                rend.material.color = Color.red;
            }
        }
        else
        {
            foreach (Renderer rend in GetComponentsInChildren <Renderer>())
            {
                rend.material.color = Color.white;
            }
        }

        Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit))
        {
            Vector3 lastPos = movePos;
            if (hit.collider.tag != "Unit" && hit.collider.tag != "SpawningTank" && hit.collider.tag != "MainCamera" && hit.point.y > 12)
            {
                movePos = hit.point;
                lastPos = movePos;
            }
            else
            {
                movePos = lastPos;
            }
        }

        Vector3 activePos = new Vector3(movePos.x, movePos.y + 2, movePos.z);

        transform.position = activePos;

        if (Input.GetMouseButton(0) && !far)
        {
            spawnTank.SpawnTankAtPos(activePos);
        }

        if (Input.GetMouseButton(1))
        {
            spawnTank.CancelSpawn();
        }
    }