「Unity/Csharp/オブジェクトをドラッグ」の版間の差分
ナビゲーションに移動
検索に移動
ページの作成:「Blockオブジェクトに以下コードをアタッチ(Add Component)するとドラッグでオブジェクトが動かせるようになる using System.Collections;...」 |
編集の要約なし |
||
| 1行目: | 1行目: | ||
画像オブジェクトにColliderをつけ以下コードをアタッチ(Add Component)するとドラッグでオブジェクトが動かせるようになる | |||
using System.Collections; | using System.Collections; | ||
using System.Collections.Generic; | using System.Collections.Generic; | ||
2017年9月17日 (日) 11:47時点における版
画像オブジェクトに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;
}
}