Last active
June 10, 2016 06:00
-
-
Save AnhPham/c127dfd4a4f1b5c2fdcf32dbc9eadbb6 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
using UnityEngine; | |
using System.Collections; | |
/// <summary> | |
/// Create a new scene, drag this script to any game object, then Play. | |
/// </summary> | |
public class ItemHolder : MonoBehaviour | |
{ | |
#region Private members | |
/// <summary> | |
/// The surface which an item is moved on (while dragging mouse). | |
/// </summary> | |
GameObject m_Surface; | |
/// <summary> | |
/// The item which is being held by mouse. | |
/// </summary> | |
Transform m_Item; | |
/// <summary> | |
/// The offset from center of an item to the click-point. | |
/// </summary> | |
Vector3 m_Offset; | |
/// <summary> | |
/// The layer mask of surface (using for Physics.Raycast). | |
/// </summary> | |
int m_SurfaceLayerMask; | |
/// <summary> | |
/// The layer mask of items (using for Physics.Raycast). | |
/// </summary> | |
int m_ItemLayerMask; | |
#endregion | |
#region Unity cycle | |
void Start() | |
{ | |
CreateItems(); | |
CreateSurface(); | |
} | |
void Update() | |
{ | |
CheckMouseDown(); | |
CheckMouseUp(); | |
CheckMouseDrag(); | |
} | |
#endregion | |
#region Start methods | |
void CreateItems() | |
{ | |
for (int i = 0; i < 10; i++) | |
{ | |
CreateItem(i); | |
} | |
} | |
void CreateSurface() | |
{ | |
m_Surface = GameObject.CreatePrimitive(PrimitiveType.Quad); | |
m_Surface.name = "[SURFACE]"; | |
m_Surface.layer = 1; | |
m_Surface.transform.localScale = Vector3.one * 30; | |
m_Surface.GetComponent<Renderer>().enabled = false; | |
m_SurfaceLayerMask = 1 << m_Surface.layer; | |
} | |
#endregion | |
#region CreateItem methods | |
void CreateItem(int index) | |
{ | |
GameObject item = GameObject.CreatePrimitive(GetShape(index)); | |
item.layer = 0; | |
item.transform.position = RandomPosition(); | |
m_ItemLayerMask = 1 << item.layer; | |
} | |
PrimitiveType GetShape(int index) | |
{ | |
int shapeIndex = index % 3; | |
switch (shapeIndex) | |
{ | |
case 0: | |
return PrimitiveType.Sphere; | |
case 1: | |
return PrimitiveType.Cube; | |
case 2: | |
return PrimitiveType.Cylinder; | |
} | |
return PrimitiveType.Cube; | |
} | |
Vector3 RandomPosition() | |
{ | |
return new Vector3(Random.Range(-2f, 2f), Random.Range(-2f, 2f), Random.Range(-2f, 2f)); | |
} | |
#endregion | |
#region Update methods | |
void CheckMouseDown() | |
{ | |
if (Input.GetMouseButtonDown(0)) | |
{ | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit; | |
// Raycast to items only to check which item will be held (when mouse down). | |
if (Physics.Raycast(ray, out hit, 100, m_ItemLayerMask)) | |
{ | |
m_Item = hit.collider.gameObject.transform; | |
m_Offset = hit.point - m_Item.position; | |
m_Surface.transform.position = hit.point; | |
} | |
} | |
} | |
void CheckMouseUp() | |
{ | |
if (Input.GetMouseButtonUp(0)) | |
{ | |
m_Item = null; | |
} | |
} | |
void CheckMouseDrag() | |
{ | |
if (Input.GetMouseButton(0)) | |
{ | |
if (m_Item != null) | |
{ | |
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); | |
RaycastHit hit; | |
// Raycast to the surface only to determine new position of the item (while dragging mouse). | |
if (Physics.Raycast(ray, out hit, 100, m_SurfaceLayerMask)) | |
{ | |
m_Item.position = hit.point - m_Offset; | |
} | |
} | |
} | |
} | |
#endregion | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment