この文書の現在のバージョンと選択したバージョンの差分を表示します。
次のリビジョン | 前のリビジョン | ||
wiki:unity:tips:040 [2014/11/13 07:11] 127.0.0.1 外部編集 |
wiki:unity:tips:040 [2015/02/23 12:44] (現在) |
||
---|---|---|---|
ライン 1: | ライン 1: | ||
- | ====== 040 カメラの向きに合わせてGameObjectを移動させる ====== | + | [[http://stepism.sakura.ne.jp/unity/wiki/|Unityはこちらに移動しました。]] |
- | + | ||
- | ===== 概要 ===== | + | |
- | カメラの向きに合わせて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> | + | |
- | + |