毎日出題の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