2
1

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 C# 超初歩2 移動コントロール

Last updated at Posted at 2018-05-13

移動させたりコントロールしたり

オブジェクトを動かしたりする。座標にアクセスしてテレポートさせるみたいな感じだけど、どちらかというと「力を加える」という考え方。とりあえず的な解釈として。

超カンタンな移動スクリプト

transform_1.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class transform_1 : MonoBehaviour {
    public float x = 0.1f;
    public float y = 0.1f;
    public float z = 0.1f;
 
    // Use this for initialization
    void Start () {
    }
 
    // Update is called once per frame
    void Update () {
        transform.position += (new Vector3(x,y,z));
    }
}

float は小数。その値には必ず f をつけておくこと。x、y、zに移動させたいスピード値を書く。するとx軸、y軸、z軸へそのスピード値が加算されてピューっとどこかへすっ飛んでいく感じで移動する。大きければ大きいほどスピード速くなる。

transform.position += (new Vector3(x軸,y軸,z軸));

超カンタンな動くスクリプト。コントロールできるよ

transform_cnt.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class transform_cnt : MonoBehaviour {
    public float x = 0.1f;
    public float y = 0.1f;
    public float z = 0.1f;
 
    // Use this for initialization
    void Start () {
    }
 
    // Update is called once per frame
    void Update () {
        if (Input.GetKey (KeyCode.LeftArrow)) {
            print ("左押したよ");
            transform.position -= (new Vector3 (x, 0, 0));
        }
        if (Input.GetKey (KeyCode.RightArrow)) {
            print ("右押したよ");
            transform.position += (new Vector3 (x, 0, 0));
        }
        if (Input.GetKey (KeyCode.UpArrow)) {
            print ("上押したよ");
            transform.position += (new Vector3 (0, 0, z));
        }
        if (Input.GetKey (KeyCode.DownArrow)) {
            print ("下押したよ");
            transform.position -= (new Vector3 (0, 0, z));
        }
    }
}

(Input.GetKey (KeyCode. ボタンとかアクション )) {
// やってほしい事
}

ここではキーボードの矢印キーを設定しただけ。ボタン押されれば、変数xとかyとかzで設定した値がプラスされる。+=で加算なので、ー=にすれば逆に減算される。

超カンタンな動くスクリプト。Time.deltaTimeで正確なFPS算定

transform_delta.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class transform_delta : MonoBehaviour {
    // 移動スピードをpublicで初期化
    public float mvsp = 3;
 
    // Use this for initialization
    void Start () {
    }
 
    // Update is called once per frame
    void Update () {
        // 左キー押されたらTime.deltaTimeとmvspでddt値を作成
        if (Input.GetKey (KeyCode.LeftArrow)) {
            float ddt = Time.deltaTime * mvsp;
            transform.position += (new Vector3 (-ddt, 0, 0));
        }
        // 右キー押されたらTime.deltaTimeとmvspでddt値を作成
        if (Input.GetKey (KeyCode.RightArrow)) {
            float ddt = Time.deltaTime * mvsp;
            transform.position += (new Vector3 (ddt, 0, 0));
        }
        if (Input.GetKey (KeyCode.UpArrow)) {
            float ddt = Time.deltaTime * mvsp;
            transform.position += (new Vector3 (0, 0, ddt));
        }
        if (Input.GetKey (KeyCode.DownArrow)) {
            float ddt = Time.deltaTime * mvsp;
            transform.position += (new Vector3 (0, 0, -ddt));
        }
    }
}

最初にmvspという変数作っておく。void Update()内で、さっきやったスクリプト内でTime.deltaTimeと掛け合わせる。Time.deltaTimeは 1フレームの時間が値として格納された関数みたいな奴で、30FPSなら1÷30=0.03333という値が格納されている。 これで再生環境に左右されず意図したスピードが再現可能。移動する系統のスクリプトでは頻出っぽい。

カンタンな動くスクリプト。Rigitbodyで質量物理計算している

rb_move1.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class rb_move1 : MonoBehaviour {
    // speedを制御する
    public float speed = 10;
 
    // Use this for initialization
    void Start () {
    }
 
    void FixedUpdate()
    {
        float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");
 
        Rigidbody situryou = GetComponent<Rigidbody>();
        situryou.AddForce(x * speed, 0, z * speed);
    }
 
    // Update is called once per frame
    void Update () {
    }
}

speedでスピードを設定しておく。void FixedUpdate()は呼び出し回数が一定しているメソッドで、キーを押してアクションする時によく使われる。(対してUpdateは一秒間内に数十回呼び出されるのでマウスを動かすような頻繁な頻度で求められるらしい)

GetCompornent<コンポーネント>で、自分のコンポーネントへアクセスする。この場合だと、このスクリプトは動かしたいナニカに貼っつけているので、そのナニカ=cubeとかsphereとかのオブジェクトを参照する。なので、物理計算するrigitbodyというコンポーネントをくっつけておく必要がある

Inspectorの「Add compornent」からphisycs→rigidbodyを選択。

rigidbody

Mass = 質量。kg(キログラム)
Drag = 空気抵抗。0で空気抵抗ゼロ
Angular Drag = 回転の空気抵抗
Use Gravity = Unityでの重力計算を使うかどうか

Rigidbodyという型の変数「situryou」にGetComponentでゲットしたデータを受け渡している。そしてそのsituryouにAddForceで力を加えている。AddForceはRigidbodyに力を加えて動かしたりする時に使う。


2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?