この文書の現在のバージョンと選択したバージョンの差分を表示します。
| 両方とも前のリビジョン 前のリビジョン | |||
|
wiki:unity:tips:040 [2015/01/06 14:14] step |
— (現在) | ||
|---|---|---|---|
| ライン 1: | ライン 1: | ||
| - | ====== 040 カメラの向きに合わせてGameObjectを移動させる ====== | ||
| - | |||
| - | ===== 概要 ===== | ||
| - | カメラの向きに合わせてGameObjectを操作します。 | ||
| - | TransformDirectionを使えばGameObjectの方向ベクトルが取得できます。 | ||
| - | |||
| - | <code csharp> | ||
| - | 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 ); | ||
| - | } | ||
| - | } | ||
| - | </code> | ||
| - | |||