「Unity/Csharp/オブジェクトをドラッグ」の版間の差分

提供: 初心者エンジニアの簡易メモ
ナビゲーションに移動 検索に移動
編集の要約なし
編集の要約なし
10行目: 10行目:
  void OnMouseDrag()
  void OnMouseDrag()
  {
  {
  Vector3 objectPointInScreen
  Vector3 objectPointInScreen = Camera.main.WorldToScreenPoint(this.transform.position);
= Camera.main.WorldToScreenPoint(this.transform.position);
  Vector3 mousePointInScreen = new Vector3(Input.mousePosition.x,
  Vector3 mousePointInScreen
= new Vector3(Input.mousePosition.x,
  Input.mousePosition.y,
  Input.mousePosition.y,
  objectPointInScreen.z);
  objectPointInScreen.z);

2017年10月16日 (月) 05:14時点における版

画像オブジェクトにColliderをつけ以下コードをアタッチ(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/