それぞれの移動方法についての違いや使い分け方、動きの確認ができるサンプルコードを以下に示します。
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);
}
}
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);
}
}
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);
}
}
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);
}
}
using UnityEngine;
public class AnimationMovement : MonoBehaviour {
public Animator animator;
void Update() {
float move = Input.GetAxis("Vertical");
animator.SetFloat("Speed", move);
}
}
Animator ControllerにはSpeed
というFloatパラメーターを設定して、移動に応じてアニメーションを制御するようにします。
using UnityEngine;
using DG.Tweening;
public class DOTweenMovement : MonoBehaviour {
void Start() {
transform.DOMove(new Vector3(0, 0, 5), 2f).SetEase(Ease.OutQuad);
}
}
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);
}
}
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;
}
}
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);
}
}
}
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; // ハンドル操作をしたときに角度が変わるか?
}
これらのサンプルコードは、それぞれの移動手法の基本的な使い方を示しています。具体的なプロジェクトや目的に応じて、適切な手法を選択し、組み合わせて使用することが重要です。各手法の特性を理解し、必要に応じてカスタマイズすることで、より効果的な移動システムを構築できるでしょう。