移動させたりコントロールしたり
オブジェクトを動かしたりする。座標にアクセスしてテレポートさせるみたいな感じだけど、どちらかというと「力を加える」という考え方。とりあえず的な解釈として。
超カンタンな移動スクリプト
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軸));
超カンタンな動くスクリプト。コントロールできるよ
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算定
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で質量物理計算している
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に力を加えて動かしたりする時に使う。