LoginSignup
0
0

More than 5 years have passed since last update.

Unity入門メモ キャラとかバーとか上下左右に移動したいコード

Last updated at Posted at 2015-08-07

スクリーンショット 2015-08-07 15.52.42.png

Unity入門してる方、一緒にがんばりましょ〜

Vector3型の移動する力作って、AddForce!

sample

using UnityEngine;

public class BarController : MonoBehaviour {

    // 動くスピード
    public float speed;


    // Update同様、毎フレーム呼ばれる(物理演算が更新されるタイミング)
    void FixedUpdate(){

        // 水平方向のキー入力取得
        float hForce = Input.GetAxis("Horizontal");

        // 垂直方向のキー入力取得
        float vForce = Input.GetAxis("Vertical");

        // 動く力生成
        Vector3 moveForce = new Vector3(hForce,  0, vForce) * speed;

        // 力を加える 
        GetComponent<Rigidbody>().AddForce(moveForce);

    }
}

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