示例#1
0
    void _movment()
    {
        //生物站立處為不可尋路點
        GameObject.Find("aStart").GetComponent <Grid>().grid[0, 0].walkable = false;

        float SphereDistance = 0;

        SphereDistance = Vector3.Distance(this.transform.position, Sphere3);
        if (SphereDistance > 0.5f)
        {
            this.bioAnimation = "mWalk";
        }

        //如果使用者操作搖桿
        if (maingameCS.clickStart &&
            isPlayer &&
            maingameCS.hitUIObjectName == "moveStick")
        {
            this.bioAnimation = "mWalk";

            //自搖桿取得的移動向量直
            Sphere.x = maingameCS.mouseDragVector.x;
            Sphere.z = maingameCS.mouseDragVector.z;

            //轉換搖桿向量至角色位置
            float Angle = MathS.AngleBetweenVector3(Sphere, new Vector3(0, Sphere.y, 0)) - 270;
            float r     = Vector3.Distance(Sphere, new Vector3(0, Sphere.y, 0));

            float tempAngel = Vector3.Angle(maingameCS.mainCamera.transform.forward, (Sphere - this.transform.position));
            tempAngel = -maingameCS.mainCamera.transform.eulerAngles.y * Mathf.PI / 180;

            Vector3 temp = MathS.getCirclePath(this.transform.position, this.transform.position, Angle + tempAngel * Mathf.Rad2Deg, r);
            Sphere2.x = temp.x;
            Sphere2.z = temp.z;
            Sphere2.y = this.transform.position.y;

            // //將spere2依據攝影機位置做座標轉換
            // float tempAngel = Vector3.Angle (maingameCS.mainCamera.transform.forward, (Sphere - this.transform.position));
            // tempAngel = -maingameCS.mainCamera.transform.eulerAngles.y * Mathf.PI / 180;
            // Sphere2.x = Sphere.x * Mathf.Cos (tempAngel) - Sphere.z * Mathf.Sin (tempAngel) + this.transform.position.x;
            // Sphere2.z = Sphere.x * Mathf.Sin (tempAngel) + Sphere.z * Mathf.Cos (tempAngel) + this.transform.position.z;
            // Sphere2.y = this.transform.position.y;

            SphereDistance = Vector3.Distance(this.transform.position, Sphere2);
        }
        //如果使用者點擊螢幕操作
        else
        {
            var Sphere2Distance = Vector3.Distance(this.transform.position, Sphere3);

            if (this.bioAnimation == "mWalk")
            {
                //如果Sphere3距離低於1,或是與Sphere3之間沒有阻礙時
                if (SphereDistance < 1 ||
                    maingameCS.PathfindingCS.decteBetween(this.transform.position, Sphere3))
                {
                    Sphere2 = Sphere3;
                }
                else
                {
                    if (Sphere2Distance > 1)
                    {
                        // Debug.Log ("PathfindingCS");
                        Sphere2 = maingameCS.PathfindingCS.FindPath_Update(this.transform.position, Sphere3);
                        if (Sphere2 == new Vector3(-999, -999, -999))
                        {
                            maingameCS.logg("<b>" + this.name + "</b>: 目前無法移動至該處");
                            Sphere2 = this.transform.position;
                            Sphere3 = this.transform.position;
                        }
                    }
                }

                //如果有人在我正前方的人是同陣營時
                //我會停下來
                // keepDistWithFrontBio(Sphere2Distance);
            }
        }

        if (this.bioAnimation == "mWalk")
        {
            //將生物轉向目標
            this.transform.position = new Vector3(this.transform.position.x, 0.5f, this.transform.position.z);
            Sphere2.y = this.transform.position.y;
            Sphere.y  = this.transform.position.y;

            faceTarget(Sphere2, rotateSpeed);

            //依照目標距離調整移動速度
            moveSpeed = moveSpeedMax;
            if (SphereDistance < 0.5f)
            {
                moveSpeed = 0.01f + moveSpeed * (SphereDistance / 0.5f);
                if (SphereDistance < 0.03f)
                {
                    this.bioAnimation = "mWait";
                    Sphere2           = Sphere3;
                }
            }

            //更新動態碰撞物
            dynamicCollision();

            //移動生物到目標點
            this.transform.position = Vector3.MoveTowards(this.transform.position, Sphere2, moveSpeed * Time.deltaTime * 50);

            //調整步伐
            anim["Walk"].speed = 1f + WalkSteptweek * moveSpeed;
        }
    }