このスクリプトは自由座標に移動する
実際には決まったMatrixにスナップするように,数値を丸めたほうが扱いやすい
その際に下のPlaneにグリッドを表示させ,そのグリッドを光らせるなどの対処が必要かど
スクリプト
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dragPoint : MonoBehaviour {
RaycastHit hit;
Ray ray;
Vector3 currentPos;//最終位置保存用変数
void OnMouseDown(){ //マウスクリック開始時用
currentPos = this.transform.position;//上昇前の値を入れる
this.transform.position = currentPos + new Vector3 (0f, 1f, 0f);//Y軸に1だけ上昇させる
currentPos = this.transform.position;//上昇した値を入れる
}
void OnMouseDrag(){//マウスドラッグ時
ray = Camera.main.ScreenPointToRay(Input.mousePosition);//マウスクリックポジションをrayで取得する
if (Physics.Raycast(ray, out hit, 100f)){//rayが当たった位置をhitに入れる
this.transform.position = new Vector3 (hit.point.x, currentPos.y, hit.point.z);//XとZ座標だけをhitの座標にする
currentPos = this.transform.position;//最終位置を変数に入れておく
}
}
void OnMouseUp(){//マウスクリック終了時
this.transform.position = currentPos - new Vector3 (0f, 1f, 0f);//最終位置からY軸に1だけ下降させる
}
}
追記
21行目を
this.transform.position = new Vector3 (Mathf.Floor(hit.point.x), currentPos.y, Mathf.Floor(hit.point.z));//
のようにMathf.Floorで小数点以下を丸め(切り捨て)ると,1m単位で動きます
実行結果