示例#1
0
 void DefaultUpdate()
 {
     if (Input.GetMouseButtonDown(0))
     {
         RaycastHit hitInfo = new RaycastHit();
         bool       hit     = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
         Ray        ray     = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (hit && hitInfo.transform.gameObject.tag == "Pawn")
         {
             //назначение куда бежать
             CurrentState = State.SelectedPawn;
             movingPawn   = hitInfo.transform.gameObject;
         }
         else
         {
             if (selectedAsset != null)
             {
                 Plane hPlane   = new Plane(Vector3.up, Vector3.zero);
                 float distance = 0;
                 if (hPlane.Raycast(ray, out distance))
                 {
                     GameObject go2 = Field.getClosestTile(ray.GetPoint(distance));
                     if (go2 != null)
                     {
                         GameObject go = Instantiate(selectedAsset, new Vector3(0, 0, 0), Quaternion.identity);
                         go.transform.position = go2.transform.position;
                     }
                 }
                 selectedAsset = null;
             }
         }
     }
 }
    private void ControllPawn()
    {
        RaycastHit hitInfo = new RaycastHit();
        bool       hit     = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);

        if (hitInfo.transform.gameObject.tag == "Ground")
        {
            Ray   ray      = Camera.main.ScreenPointToRay(Input.mousePosition);
            Plane hPlane   = new Plane(Vector3.up, Vector3.zero);
            float distance = 0;
            if (hPlane.Raycast(ray, out distance))
            {
                GameObject go2 = Field.getClosestTile(ray.GetPoint(distance));
                if (go2 != null)
                {
                    PawnBehaviour pb = gameObjectClicked.GetComponent <PawnBehaviour>();
                    pb.MoveOnTile(go2);
                }
            }
        }
        else if (hitInfo.transform.gameObject.tag == "Pawn")
        {
            gameObjectClicked = hitInfo.transform.gameObject;
        }
    }
示例#3
0
    public void Execute()
    {
        if (IsComplete)
        {
            return;
        }
        if (path == null)
        {
            _from = _field.getClosestTile(gameObject.transform.position).GetComponent <TileBehaviour>();
            path  = GetPath(new List <TileBehaviour>()
            {
                _from, _to
            });
            curIdx = 0;
        }

        if (path.Count > 0)
        {
            var pathLength = _speed * Time.deltaTime;
            var curPos     = gameObject.transform.position;
            var segment    = (path[curIdx] - curPos).magnitude;
            if ((curPos - path[path.Count - 1]).sqrMagnitude > 0.01f)
            {
                while (segment < pathLength && curIdx + 1 < path.Count)
                {
                    segment += (path[curIdx + 1] - path[curIdx]).magnitude;
                    curIdx++;
                }
                if (pathLength > segment && curIdx + 1 >= path.Count)
                {
                    gameObject.transform.position = path[path.Count - 1];
                }
                else if (curIdx > 0)
                {
                    gameObject.transform.position = path[curIdx] + (path[curIdx - 1] - path[curIdx]).normalized * (segment - pathLength);
                    gameObject.transform.forward  = (path[curIdx] - path[curIdx - 1]).normalized;
                }
                else if (curIdx == 0)
                {
                    gameObject.transform.position = path[curIdx] + (curPos - path[0]).normalized * (segment - pathLength);
                    gameObject.transform.forward  = (path[0] - curPos).normalized;
                }
                gameObject.GetComponent <Animator>().SetInteger("moving", 1);
            }
            else
            {
                gameObject.GetComponent <Animator>().SetInteger("moving", 0);
                IsComplete = true;
            }
        }
    }