ClickSpawner.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. public class ClickSpawner : MonoBehaviour
  4. {
  5. public GameObject spherePrefab;
  6. private GameObject spawnedSphere;
  7. void Update()
  8. {
  9. if (Mouse.current.leftButton.wasPressedThisFrame)
  10. {
  11. Ray ray = Camera.main.ScreenPointToRay(Mouse.current.position.ReadValue());
  12. RaycastHit hit;
  13. if (Physics.Raycast(ray, out hit))
  14. {
  15. if (hit.transform == this.transform)
  16. {
  17. if (spawnedSphere == null)
  18. {
  19. spawnedSphere = Instantiate(spherePrefab,
  20. transform.position + new Vector3(0.15f, 0, 0),
  21. Quaternion.identity);
  22. spawnedSphere.transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
  23. }
  24. else
  25. {
  26. Destroy(spawnedSphere);
  27. spawnedSphere = null;
  28. }
  29. }
  30. }
  31. }
  32. }
  33. }