メインコンテンツまでスキップ

Move

それぞれの移動方法についての違いや使い分け方、動きの確認ができるサンプルコードを以下に示します。

1. Transform

特徴

  • 最も基本的な移動方法で、直接オブジェクトの位置を設定する。
  • 物理演算の影響を受けないため、瞬時に位置を変更できる。

サンプルコード

using UnityEngine;

public class TransformMovement : MonoBehaviour {
public float moveSpeed = 5f;

void Update() {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(movement * moveSpeed * Time.deltaTime);
}
}


2. Rigidbody

特徴

  • 物理演算を利用した移動方法。力やトルクを使ってオブジェクトを動かす。
  • 自然な物理挙動を持つ。

サンプルコード

using UnityEngine;

public class RigidbodyMovement : MonoBehaviour {
public float moveForce = 10f;
private Rigidbody rb;

void Start() {
rb = GetComponent<Rigidbody>();
}

void FixedUpdate() {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * moveForce);
}
}


3. CharacterController

特徴

  • キャラクターの移動に特化したコンポーネント。
  • 衝突判定が簡単で、物理演算の影響を受けない。

サンプルコード

using UnityEngine;

public class CharacterControllerMovement : MonoBehaviour {
public float moveSpeed = 5f;
private CharacterController controller;

void Start() {
controller = GetComponent<CharacterController>();
}

void Update() {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
controller.Move(movement * moveSpeed * Time.deltaTime);
}
}


特徴

  • AIやNPCの移動に適している。ナビゲーションメッシュを使って経路を計算。
  • OffMeshLinkを使うと、特定の移動パスを持つNPCを実装できる。

サンプルコード

using UnityEngine;
using UnityEngine.AI;

public class NavMeshMovement : MonoBehaviour {
public Transform target;
private NavMeshAgent agent;

void Start() {
agent = GetComponent<NavMeshAgent>();
}

void Update() {
agent.SetDestination(target.position);
}
}


5. Animation

特徴

  • アニメーションを用いてオブジェクトを動かす方法。自然な動きのキャラクターを作るために重要。

サンプルコード

using UnityEngine;

public class AnimationMovement : MonoBehaviour {
public Animator animator;

void Update() {
float move = Input.GetAxis("Vertical");
animator.SetFloat("Speed", move);
}
}

Animator ControllerにはSpeedというFloatパラメーターを設定して、移動に応じてアニメーションを制御するようにします。



6. DOTween

特徴

  • Tweenライブラリを使い、スムーズな移動やフェードイン・アウトを実現する。

サンプルコード

using UnityEngine;
using DG.Tweening;

public class DOTweenMovement : MonoBehaviour {
void Start() {
transform.DOMove(new Vector3(0, 0, 5), 2f).SetEase(Ease.OutQuad);
}
}


7. Spline Movement

特徴

  • スプライン曲線に沿った移動ができる。カスタムパスに沿った移動を実現。

サンプルコード

using UnityEngine;

public class SplineMovement : MonoBehaviour {
public Transform[] points;
public float speed = 1f;
private float t = 0f;

void Update() {
t += Time.deltaTime * speed;
t = Mathf.Clamp01(t);
transform.position = GetPointOnSpline(t);
}

Vector3 GetPointOnSpline(float t) {
int p0 = Mathf.FloorToInt(t * (points.Length - 1));
int p1 = (p0 + 1) % points.Length;
return Vector3.Lerp(points[p0].position, points[p1].position, t * (points.Length - 1) - p0);
}
}


8. Custom Script

特徴

  • 自前でキャラクターやオブジェクトの移動を制御するスクリプトを書く方法。独自の物理演算や動作を実装できる。

サンプルコード

using UnityEngine;

public class CustomScriptMovement : MonoBehaviour {
public float speed = 5f;

void Update() {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");

Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.position += movement * speed * Time.deltaTime;
}
}


9. Physics.Simulate

特徴

  • 物理エンジンのシミュレーションをカスタマイズするための手法。物理の挙動を直接制御する。

サンプルコード

using UnityEngine;

public class PhysicsSimulateMovement : MonoBehaviour {
private Rigidbody rb;

void Start() {
rb = GetComponent<Rigidbody>();
}

void Update() {
if (Input.GetKeyDown(KeyCode.Space)) {
Physics.Simulate(Time.fixedDeltaTime);
}
}
}


10. WheelCollider

特徴

  • 車両の物理挙動を制御するための特化したコンポーネント。特に車両シミュレーションに必要。

サンプルコード

using UnityEngine;

public class WheelColliderMovement : MonoBehaviour {
public List<AxleInfo> axleInfos;
public float maxMotorTorque = 1500f;
public float maxSteeringAngle = 30f;

void FixedUpdate() {
float motor = maxMotorTorque * Input.GetAxis("Vertical");
float steering = maxSteeringAngle * Input.GetAxis("Horizontal");

foreach (AxleInfo axleInfo in axleInfos) {
if (axleInfo.steering) {
axleInfo.leftWheel.steerAngle = steering;
axleInfo.rightWheel.steerAngle = steering;
}
if (axleInfo.motor) {
axleInfo.leftWheel.motorTorque = motor;
axleInfo.rightWheel.motorTorque = motor;
}
ApplyLocalPositionToVisuals(axleInfo.rightWheel);
ApplyLocalPositionToVisuals(axleInfo.leftWheel);
}
}

public void ApplyLocalPositionToVisuals(WheelCollider collider) {
Transform visualWheel = collider.transform.GetChild(0);
Vector3 position;
Quaternion rotation;
collider.GetWorldPose(out position, out rotation);
visualWheel.position = position;
visualWheel.rotation = rotation;
}
}

[System.Serializable]
public class AxleInfo {
public WheelCollider leftWheel;
public WheelCollider rightWheel;
public bool motor; // 駆動輪か?
public bool steering; // ハンドル操作をしたときに角度が変わるか?
}


まとめ

これらのサンプルコードは、それぞれの移動手法の基本的な使い方を示しています。具体的なプロジェクトや目的に応じて、適切な手法を選択し、組み合わせて使用することが重要です。各手法の特性を理解し、必要に応じてカスタマイズすることで、より効果的な移動システムを構築できるでしょう。