0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Unity初級]キャラの斜め移動を滑らかにする

Posted at

概要

上下左右への移動距離を1とすると、工夫をしなければ斜め方向は「右に1上に1」でルート2(つまり1.47ぐらい)になる。それを1にできたら少しスムーズになる

本文


    private Vector2 moveInput;
    //public bool toggle;
    void Update()
    {
        moveInput.x = Input.GetAxisRaw("Horizontal");
        moveInput.y = Input.GetAxisRaw("Vertical");

        //if (toggle == true){
            moveInput.Normalize();
        //}
        //Debug.Log(moveInput.x);

        character.velocity = moveInput;
    }

.Normalize()をすることで斜め方向が自然な移動量に調整されます(上が1右が1なら斜めも1)

本文

もし興味があるなら、コメントアウトしてある4行を解除してInspectorのtoggleチェックボックスをON/OFFしてみてください。Consoleに流れる数字が変化するので.Normalize()の作用を確認できます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?