====== 040 カメラの向きに合わせてGameObjectを移動させる ====== ===== 概要 ===== カメラの向きに合わせてGameObjectを操作します。 TransformDirectionを使えばGameObjectの方向ベクトルが取得できます。 using UnityEngine; using System.Collections; public class PlayerScript : MonoBehaviour { CharacterController controller; Vector3 moveDirection; float fSpeed = 3.0f; void Start () { controller = GetComponent("CharacterController") as CharacterController; } void Update () { Vector3 forward = Camera.mainCamera.transform.TransformDirection( Vector3.forward ); Vector3 right = Camera.mainCamera.transform.TransformDirection( Vector3.right ); moveDirection = Input.GetAxis("Horizontal") * right + Input.GetAxis("Vertical") * forward; moveDirection *= fSpeed; // 移動 controller.Move( moveDirection * Time.deltaTime ); } }