예제 #1
0
파일: Unit.cs 프로젝트: kyapp69/Unity-RTS-1
    void Update()
    {
        //target things
        targetSearchTimer += Time.deltaTime;
        reloadTimer       += Time.deltaTime;
        if (target && team != 2)
        {
            rangeToTarget = (target.position - transform.position).magnitude;
            if (rangeToTarget < range)
            {
                nav.enabled = false;
                Vector3 targetDirHor  = new Vector3(target.position.x, transform.position.y, target.position.z) - transform.position;
                float   angleHor      = Vector3.Angle(targetDirHor, transform.forward);
                Vector3 targetDirVert = target.position - gun.position;
                float   angleVert     = Vector3.Angle(targetDirVert, gun.forward);
                if (angleHor < 30 && angleVert < 30 && reloadTimer > reloadTime)
                {
                    moving = false;
                    //fire
                    GameObject shot = (GameObject)Instantiate(RhinoShot, fireLocation.position, gun.rotation);
                    shot.GetComponent <RhinoShotScript>().team = team;
                    reloadTimer = 0f;
                }
                if (angleHor > 1)
                {
                    //turn toward target
                    Quaternion lookAtRotation = Quaternion.LookRotation(new Vector3(target.position.x, transform.position.y, target.position.z) - transform.position);
                    // Will assume you mean to divide by damping meanings it will take damping seconds to face target assuming it doesn't move
                    transform.rotation = Quaternion.Slerp(transform.rotation, lookAtRotation, Time.deltaTime / 1.0f);
                }
                if (angleVert > 1)
                {
                    Quaternion lookAtRotation = Quaternion.LookRotation(target.position - gun.position);
                    gun.rotation = Quaternion.Slerp(gun.rotation, lookAtRotation, Time.deltaTime / 1.0f);
                }
            }
            else
            {
                nav.enabled = true;
            }
        }
        //find targets in range
        //only looks for target every second if it does not already have a target in range.
        if (targetSearchTimer > targetSearchTime && team == 1 && !building && rangeToTarget < range)
        {
            targetSearchTimer = 0f;
            GameObject[] targets       = GameObject.FindGameObjectsWithTag("Enemy");
            int          minRangeIndex = -1;
            float        minRange      = Mathf.Infinity;
            for (int i = 0; i < targets.Length; i++)
            {
                float targetRange = (targets [i].GetComponent <Transform> ().position - transform.position).magnitude;
                if (targetRange < minRange)
                {
                    minRange      = targetRange;
                    minRangeIndex = i;
                }
            }
            if (minRange < range)
            {
                target = targets[minRangeIndex].GetComponent <Transform>();
            }
            else               //<There is no target in range
            {
                target = null;
            }
        }



        //handes movable unit specific things
        if (!building)
        {
            //set animation state
            anim.SetBool("moving", moving);
            //sets moving to false if reached target destination
            if (transform.position == destination)
            {
                moving = false;
            }
            if (moving)
            {
                //turns slowly until facing is within tolerance of desired facing
                Vector3 targetDir = new Vector3(nav.steeringTarget.x, transform.position.y, nav.steeringTarget.z) - transform.position;
                float   angle     = Vector3.Angle(targetDir, transform.forward);
                if (angle < 5.0f)
                {
                    nav.speed = 5f;
                }
                else if (angle < 45.0f)
                {
                    nav.speed = 3.5f;
                }
                else if (angle > 150)
                {
                    nav.speed = 0.5f;
                }
                else
                {
                    nav.speed = 2;
                }
            }
        }

        //if right mouse button is clicked, set the mouse position as the destination for the nav mesh agent.
        if (selected && Input.GetMouseButtonDown(1) && !building && Input.mousePosition.y > RTSCamera.mouseYLowerBound && team == 1)
        {
            if (moving && nav.enabled == true)
            {
                nav.SetDestination(transform.position);
            }
            startPosition = transform.position - camera.GetGroupCenter();
            Vector3 hitPoint = FindHitPoint();
            if (hitPoint != null)
            {
                float x = hitPoint.x;
                float y = hitPoint.y;
                float z = hitPoint.z;
                destination = new Vector3(x, y, z) + startPosition;
                MakeMove();
            }
        }

        //If the unit is within the selection drag box when the left mouse button is released then select the unit
        if (GetComponentInChildren <Renderer>().isVisible&& Input.GetMouseButtonUp(0) && RTSCamera.selecting && team == 1)
        {
            Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
            camPos.y = RTSCamera.InvertMouseY(camPos.y);
            bool s;
            if (RTSCamera.selection.width > 0 && RTSCamera.selection.height > 0)
            {
                s = RTSCamera.selection.Contains(camPos);
            }
            else
            {
                s = new Rect(camera.mousePosX, RTSCamera.InvertMouseY(camera.mousePosY), -RTSCamera.selection.width, -RTSCamera.selection.height).Contains(camPos);
            }
            if (s != selected)
            {
                setSelected();
            }
        }

        //unselects the unit if the left mouse button is clicked, can be reselected in the same click
        if (Input.GetMouseButtonDown(0) && selected && team == 1)
        {
            GameObject hitObject = FindHitObject();
            if (hitObject != transform.gameObject)
            {
                setSelected();
            }
        }
    }