Skip to content

Instantly share code, notes, and snippets.

@s2kw
Created June 15, 2016 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s2kw/58b6a22daa125b34a1af882b43229300 to your computer and use it in GitHub Desktop.
Save s2kw/58b6a22daa125b34a1af882b43229300 to your computer and use it in GitHub Desktop.
毎日出題の3日目第一問の答え
public class CreatePrimitiveByMouse : MonoBehaviour {
GameObject primitive = null;
void Update () {
if (Input.GetMouseButton(0))
{
// マウス位置から発生するRayを作成
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// 当たり確認オブジェクト
RaycastHit hit;
if (Physics.Raycast(ray, out hit, float.MaxValue))
{
if (primitive == null)
{
primitive = GameObject.CreatePrimitive(PrimitiveType.Cube);
Destroy(primitive.GetComponent<Collider>());
}
primitive.SetActive(true);
primitive.transform.position = hit.point;
}
}
else
if (Input.GetMouseButtonUp(0)) {
if (primitive != null)
{
primitive.SetActive(false);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment