quhluh

Untitled

May 30th, 2023
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class ObjectPlacementAndDestruction : MonoBehaviour
  4. {
  5. public GameObject objectPrefab;
  6.  
  7. void Update()
  8. {
  9. // Place the object at the mouse position when the left mouse button is clicked
  10. if (Input.GetMouseButtonDown(0))
  11. {
  12. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  13. RaycastHit hit;
  14.  
  15. if (Physics.Raycast(ray, out hit))
  16. {
  17. Vector3 spawnPosition = hit.point;
  18. Instantiate(objectPrefab, spawnPosition, Quaternion.identity);
  19. }
  20. }
  21.  
  22. // Destroy the object at the mouse position when the right mouse button is clicked
  23. if (Input.GetMouseButtonDown(1))
  24. {
  25. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  26. RaycastHit hit;
  27.  
  28. if (Physics.Raycast(ray, out hit))
  29. {
  30. GameObject objectToDestroy = hit.collider.gameObject;
  31. Destroy(objectToDestroy);
  32. }
  33. }
  34. }
  35. }
Add Comment
Please, Sign In to add comment