コード例 #1
0
    // Update is called once per frame
    void Update()
    {
        float angle = 0;
        bool  player_stand;             //是否站在能够旋转的方块上
        bool  player_stand_this;        //所站位置是否为当前方块

        GameObject    player = GameObject.Find("Player");
        MoveCharacter script = player.GetComponent("MoveCharacter") as MoveCharacter;

        player_stand      = (script.StandingFloor.GetComponent("Rotating") != null);
        player_stand_this = (script.StandingFloor == gameObject);

        if (!playerRot && player_stand)
        {
            return;                                             //判断playerRot能够允许旋转,能,旋转;不能,不旋转不能动;
        }
        //一下代码的前提都是playerRot能够允许旋转

        if (Input.GetKey(KeyCode.Q))
        {
            angle = -Time.deltaTime * 128;
        }
        else if (Input.GetKey(KeyCode.W))
        {
            angle = Time.deltaTime * 128;
        }
        else
        {
            float dst = Mathf.Floor((m_Angle + 45) / 90) * 90;
            angle = dst - m_Angle;
            if (Mathf.Abs(angle) > 1)
            {
                angle *= 0.125f;
            }
            //Debug.Log(angle);
            if (rotating && angle == 0)
            {
                rotating = false;
                Vector3 pos = transform.position;
                transform.position = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z));
                if (player_stand_this)
                {
                    script.RecalcPlayerPos();
                }
            }
        }

        m_Angle += angle;
        while (m_Angle > 360)
        {
            m_Angle -= 360;
        }
        while (m_Angle < 0)
        {
            m_Angle += 360;
        }

        transform.RotateAround(point, axis, angle);

        if (angle != 0)
        {
            rotating = true;

            script.StopMoving();
            if (!player_stand)
            {
                script.RecalcPlayerPos();                       //如果没有站在能够旋转的方格上就   重置player的位置
            }
            if (player_stand_this)                              //如果站在当前的方块上,包括player一起旋转
            {
                player.transform.RotateAround(point, axis, angle);
            }
        }
    }
コード例 #2
0
    void Update()
    {
        if (selected)
        {
            GetComponent <Renderer>().material.color = Color.cyan;

            float angle = 0;
            bool  player_stand;
            bool  player_stand_this;

            GameObject    player = GameObject.Find("Player");
            MoveCharacter script = player.GetComponent("MoveCharacter") as MoveCharacter;
            player_stand      = (script.StandingFloor.GetComponent("Rotating") != null);
            player_stand_this = (script.StandingFloor == gameObject);

            if (!playerRot && player_stand)
            {
                return;
            }

            if (Input.GetKey(KeyCode.Q))
            {
                angle = -Time.deltaTime * 128;
            }
            else if (Input.GetKey(KeyCode.W))
            {
                angle = Time.deltaTime * 128;
            }
            else
            {
                // 修正旋转的位置
                float dst = Mathf.Floor((m_Angle + 45) / 90) * 90;
                angle = dst - m_Angle;
                if (Mathf.Abs(angle) > 1)
                {
                    angle *= 0.125f;
                }
                if (rotating && angle == 0)
                {
                    rotating = false;
                    Vector3 pos = transform.position;
                    transform.position = new Vector3(Mathf.Round(pos.x), Mathf.Round(pos.y), Mathf.Round(pos.z));
                    if (player_stand_this)
                    {
                        script.RecalcPlayerPos();
                    }
                }
            }

            m_Angle += angle;
            while (m_Angle > 360)
            {
                m_Angle -= 360;
            }
            while (m_Angle < 0)
            {
                m_Angle += 360;
            }


            // 判断玩家位置是否重叠(还没实现)


            transform.RotateAround(point, axis, angle);

            if (angle != 0)
            {
                rotating = true;

                script.StopMoving();
                if (!player_stand)
                {
                    script.RecalcPlayerPos();
                }

                if (player_stand_this)
                {
                    player.transform.RotateAround(point, axis, angle);
                }
            }
        }
        else
        {
            GetComponent <Renderer>().material.color = Color.grey;
        }
    }