当前位置 : 主页 > 手机开发 > 无线 >

unity 移动

来源:互联网 收集:自由互联 发布时间:2021-06-10
上下左右平移 void Start() { QualitySettings.vSyncCount = 0 ; Application.targetFrameRate = 50 ; // 控制帧数 } // Update is called once per frame void Update() { // 移动 float horizontal = Input.GetAxis( " Horizontal " ); float v

上下左右平移

    void Start()
    {
        QualitySettings.vSyncCount = 0;
        Application.targetFrameRate = 50;//控制帧数
    }

    // Update is called once per frame
    void Update()
    {

        //移动
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");

        Vector2 position = transform.position;
        position.x = position.x + 0.1f * horizontal;
        position.y = position.y + 0.1f * vertical;
        transform.position = position;
    }

 

增量时间(Time.deltaTime)

因为设备的每秒帧数不相同,为了让不同设备在同时间内移动相同的距离,引入的增量时间的概念

        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        Vector2 position = transform.position;
        position.x = position.x + 3.0f * horizontal * Time.deltaTime;
        position.y = position.y + 3.0f * vertical * Time.deltaTime;
        transform.position = position;
网友评论