前回、Rigidbodyを使用した移動方法を試しました。
(http://lab7.blog.jp/archives/2041666.html)
(http://lab7.blog.jp/archives/2041666.html)
今回はTransformを使用した基本的な移動方法を試します。
【次の瞬間の移動先を指定する】
- position: ワールド空間の位置指定
- localPosition: 親からの相対的な位置指定 (親がない場合、position と同じ)
- Translate: 指定した方向と距離に移動
(参照元はUnityマニュアル。
https://docs.unity3d.com/jp/540/ScriptReference/Transform.html)(青:position、緑:localPosition、赤:Translate)
このC#ソースは以下です。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveTest_2 : MonoBehaviour {
Vector3 moveDirection;
void Start () {
moveDirection = transform.up;
}
void FixedUpdate() {
if (this.name == "Position")
// positionの設定
transform.position
= transform.position + moveDirection * Time.deltaTime;
else if (this.name == "LocalPosition")
// localPositionの設定
transform.localPosition
= transform.localPosition + moveDirection * Time.deltaTime;
else if (this.name == "Translate")
// Translateの設定
transform.Translate(moveDirection * Time.deltaTime);
}
}