Created
March 9, 2019 03:21
-
-
Save ryosuzuki/0d1eae508226e140cc363539db7dbae0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make sure to select "Box Colider" option at the Inspector panel | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class DragObject : MonoBehaviour { | |
//float distance = 10; | |
//private void OnMouseDrag() | |
//{ | |
// Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance); | |
// Vector3 objectPos = Camera.main.ScreenToWorldPoint(mousePos); | |
// transform.position = objectPos; | |
//} | |
private Vector3 mOffset; | |
private float mzCoord; | |
public GameObject prefab; | |
private int count = 0; | |
private void Update() | |
{ | |
if (Input.GetKeyDown(KeyCode.Space)) | |
{ | |
Vector3 pos = new Vector3(count, count, 0); | |
Instantiate(prefab, pos, Quaternion.identity); | |
//GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); | |
//cube.AddComponent<Rigidbody>(); | |
//cube.transform.position = new Vector3(count, count, 0); | |
Debug.Log("press space"); | |
count++; | |
} | |
} | |
private void OnMouseDown() { | |
mzCoord = Camera.main.WorldToScreenPoint(transform.position).z; | |
mOffset = gameObject.transform.position - GetMouseWorldPos(); | |
Debug.Log("click"); | |
} | |
private Vector3 GetMouseWorldPos() { | |
Vector3 mousePoint = Input.mousePosition; | |
mousePoint.z = mzCoord; | |
return Camera.main.ScreenToWorldPoint(mousePoint); | |
} | |
private void OnMouseDrag() { | |
transform.position = GetMouseWorldPos() + mOffset; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment