2017-03-18 12 views
0

私はUnity Gameプロジェクトに取り組んできました。私のスプライトは右に移動します。ここに私の現在のスクリプトは次のとおりです。ユニティオブジェクトの移動 - オブジェクトのみが右下に移動しますか?

#pragma strict 
function Update(){ 
    var ship = GetComponent(Rigidbody2D); 
    var speed = 10; 
    if(Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)){ 
     ship.velocity.y = speed; 
    }else { 
     ship.velocity.y = 0; 
    } 
    if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)){ 
     ship.velocity.x = -1 * speed; 
    }else { 
     ship.velocity.x = 0; 
    } 
    if(Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)){ 
     ship.velocity.y = -1 * speed; 
    }else { 
     ship.velocity.y = 0; 
    } 
    if(Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)){ 
     ship.velocity.x = speed; 
    }else { 
     ship.velocity.x = 0; 
    } 
} 

答えて

0

自分で考えてみると、最初に他のキーをテストしていなくてもベロシティを0に設定していたという問題がありました。

0

私はInput.GetAxis(「横」)とInput.GetAxis(「垂直」)を使用するように)これであなたは、コードの多くを回避し、それが良いでしょう。

そして、あなたはrigidbody2Dコンポーネントをインスタンス化する必要はありませんが、ただの操作を行います。また

rigidbody2d.velocity = new Vector2(speed * Input.GetAxis("Horizontal"), speed * Input.GetAxis("Vertical"));

が、私はあなたのコードに何らかの障害が表示されていない、あなたのオブジェクトに別のスクリプトを持っていますか?またはどのようにあなたの剛体2Dをオブジェクトに持っていますか?

関連する問題