您的位置:首页 > 其它

物体移动时按下Shift键加快速度

2018-02-06 18:08 309 查看


using UnityEngine;

using UnityEngine.UI;

public class move : MonoBehaviour {

    private float speed = 10f;//初始速度

    public bool isPPP = false;//这是一个检测的状态

    public Toggle to;//UI

    public Text toText;//文本

void Update () {

        float v = Input.GetAxis("Vertical");

        ChangeSpeed();//调用状态检测的方法

        if (isPPP == false)//判断这个状态下是初始速度

        {

           

            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))

            {

                float ratation = v * speed * Time.deltaTime;

                transform.Translate(ratation, 0, ratation);

            }

        }

        if(isPPP == true)//这个状态下是新赋予的速度

        {

            float speed = 50f;

            if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))

            {

                float ratation = v * speed * Time.deltaTime;

                transform.Translate(ratation, 0, ratation);

            }

        }

    }

    //检测状态以及改变状态的方法

    public void ChangeSpeed()

    {

        if (isPPP == false)

        {

            if (Input.GetKey(KeyCode.LeftShift))

            {

                to.GetComponent<Toggle>().isOn = true;

                toText.text = "疾風步";

                toText.GetComponent<Text>().color = Color.red;

                isPPP = true;

            }

        }

        else if (isPPP == true)

        {

            if (Input.GetKeyUp(KeyCode.LeftShift))

            {

                to.GetComponent<Toggle>().isOn = false;

                toText.text = "走路";

                toText.GetComponent<Text>().color = Color.green;

                isPPP = false;

            }

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: