//tips
//Graphicクラスとは
MonoBehaviourではなく、Graphicが用いられていたので、MonoBehaviourに変更しようとしたら、.raycastTargetの部分でエラーが生じた。
Graphicは全てのvisual UIコンポーネントの基本クラスであり、EventSystem内の機能であるGraphic Raycasterを編集するために必要であったと言える。
https://docs.unity3d.com/2018.1/Documentation/ScriptReference/UI.Graphic.html
https://docs.unity3d.com/ja/2018.4/Manual/EventSystem.html
//eventsystemを使用してドラッグを行う
EventSystemを使ってSpriteをドラッグする。
MainCameraにPysics2DRaycasterをアタッチ、EventSystemをシーンに配置、ドラッグで動かしたいオブジェクトにスクリプトとCollider2Dをアタッチする。
CameraのProjectionはOrthographicに変更。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class DragEventTest : MonoBehaviour, IDragHandler
{
public void OnDrag(PointerEventData data)
{
Vector3 TargetPos = Camera.main.ScreenToWorldPoint(data.position);
TargetPos.z = 0;
transform.position = TargetPos;
Debug.Log("1");
}
}
PointerEventData.positionでドラッグ中のPointer(タッチ位置、マウスカーソルの位置)のポジションを取得できるので、それをオブジェクトのtransform.positionに代入。
PointerEventData.positionはスクリーン座標系なので、ワールド座標系のtransform.positionに代入するには同じワールド座標系に変換する必要がある。PointerEventData.positionはVector2なので、別途こちらで代入するZ位置を指定する。
UIのimageでドラッグを行う場合はワールド座標に切り替える必要はないので、using UnityEngine.UI;を追加し、Vector3 TargetPos = data.position;としてやれば良い。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragEventTest : MonoBehaviour, IDragHandler
{
public void OnDrag(PointerEventData data)
{
Vector3 TargetPos = data.position;
TargetPos.z = 0;
transform.position = TargetPos;
Debug.Log("100");
}
}
Imageのraycasttargetにきちんとチェックが入っているかを確認。
次に、このimageの動きと他の3dオブジェクトの動きを同期させることを考える。
2つのオブジェクトを監視し、vector3で位置情報を獲得し、imageから3dオブジェクトに移動分を加算処理すれば良いと考えたが、うまく移動分が反映されないので現在確認中。
サブカメラの回転を仮想スティックで調整できるようになったら、難易度管理に経路探索アルゴリズムを作成。
https://note.com/assetstorejp/n/n3a47957cbb6c
https://qiita.com/2dgames_jp/items/f29e915357c1decbc4b7
https://gamescience.jp/2018/Paper/Tsugawa_2018.pdf