如何在unity中使用鍵盤控制物體的前后左右


      方法1:利用虛擬軸
void Update () {
        float hor = Input .GetAxis( "Horizontal" );
        float ver = Input .GetAxis( "Vertical" );
        transform.position += -transform.forward * ver * Time .deltaTime * 5;
        if (ver < 0)
        {
            hor = -hor;
        }
        transform.Rotate(transform.up * hor * Time .deltaTime * 45);
    }
 
方法2:設置鍵控制
        void Update () {
        if ( Input .GetKey( KeyCode .W)|| Input .GetKey( KeyCode .UpArrow)) {
            transform.position += -transform.forward * Time .deltaTime * 10;
        }
        if ( Input .GetKey( KeyCode .S)|| Input .GetKey( KeyCode .DownArrow))
        {
            transform.position += transform.forward * Time .deltaTime * 10;
        }
        if ( Input .GetKey( KeyCode .A)|| Input .GetKey( KeyCode .LeftArrow))
        {
            transform.Rotate(transform.up* Time .deltaTime*45);
        }
        if ( Input .GetKey( KeyCode .D)|| Input .GetKey( KeyCode .RightArrow))
        {
            transform.Rotate(-transform.up * Time .deltaTime * 45);
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM