Création d'un petit party-game anonyme de Build + Bagar sous Unity
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlockSpawner.cs 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. public class BlockSpawner : MonoBehaviour
  6. {
  7. [SerializeField]
  8. private GameObject _blockToPlace;
  9. public GameObject BlockToPlace
  10. {
  11. get
  12. {
  13. return _blockToPlace;
  14. }
  15. set
  16. {
  17. DisablePreviewMode();
  18. _blockToPlace = value;
  19. }
  20. }
  21. public float spawnDistance = 3f;
  22. private GameObject _blockInstance;
  23. private bool _isPreview = false;
  24. public void Update()
  25. {
  26. if (_blockInstance == null) return;
  27. _blockInstance.transform.position = transform.position + transform.forward * spawnDistance;
  28. _blockInstance.transform.rotation = transform.rotation;
  29. }
  30. public void OnPlaceBlock()
  31. {
  32. if (!_isPreview) return;
  33. _blockInstance = null;
  34. DisablePreviewMode();
  35. }
  36. public void OnTogglePreviewMode(InputValue value)
  37. {
  38. float valueAsFloat = value.Get<float>();
  39. if (valueAsFloat == 1f) EnablePreviewMode();
  40. else DisablePreviewMode();
  41. }
  42. private void EnablePreviewMode()
  43. {
  44. if (_blockToPlace == null) return;
  45. Vector3 pos = transform.position + transform.forward * spawnDistance;
  46. Quaternion rot = transform.rotation;
  47. _blockInstance = Instantiate(_blockToPlace, pos, rot);
  48. _isPreview = true;
  49. }
  50. private void DisablePreviewMode()
  51. {
  52. Destroy(_blockInstance);
  53. Debug.Log("disabled");
  54. _isPreview = false;
  55. }
  56. }