unity 移动

上下左右平移

    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;

猜你喜欢

转载自www.cnblogs.com/buchizaodian/p/10906606.html