Unity/Csharp/オブジェクトをドラッグ

提供: 初心者エンジニアの簡易メモ
2017年9月15日 (金) 23:03時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「Blockオブジェクトに以下コードをアタッチ(Add Component)するとドラッグでオブジェクトが動かせるようになる using System.Collections;...」)
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
ナビゲーションに移動 検索に移動

Blockオブジェクトに以下コードをアタッチ(Add Component)するとドラッグでオブジェクトが動かせるようになる

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlockScript : MonoBehaviour {
	void Start () {
	}
	void Update () {
	}
	void OnMouseDrag()
	{
		Vector3 objectPointInScreen
		= Camera.main.WorldToScreenPoint(this.transform.position);
		Vector3 mousePointInScreen
		= new Vector3(Input.mousePosition.x,
			Input.mousePosition.y,
			objectPointInScreen.z);
		Vector3 mousePointInWorld = Camera.main.ScreenToWorldPoint(mousePointInScreen);
		mousePointInWorld.z = this.transform.position.z;
		this.transform.position = mousePointInWorld;
	}
}

参考

http://neareal.com/1230/