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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. public class BlockSpawner : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. _playerInput = GetComponent<PlayerInput>();
  10. }
  11. [SerializeField]
  12. private GameObject _blockToPlace;
  13. public GameObject BlockToPlace
  14. {
  15. get
  16. {
  17. return _blockToPlace;
  18. }
  19. set
  20. {
  21. DisablePreviewMode();
  22. _blockToPlace = value;
  23. }
  24. }
  25. PlayerInput _playerInput;
  26. public float spawnDistance = 3f;
  27. public Shader previewShader;
  28. private GameObject _blockInstance;
  29. private bool _isPreview = false;
  30. public bool _blockRotating = false;
  31. private Quaternion _lastRotationState;
  32. private Shader _initialShader;
  33. public void OnPlaceBlock()
  34. {
  35. if (!_isPreview) return;
  36. _blockInstance.transform.SetParent(null);
  37. SwitchToDefaultRender();
  38. _blockInstance = null;
  39. DisablePreviewMode();
  40. }
  41. public void OnTogglePreviewMode(InputValue value)
  42. {
  43. float valueAsFloat = value.Get<float>();
  44. if (valueAsFloat == 1f)
  45. {
  46. EnablePreviewMode();
  47. }
  48. else
  49. {
  50. DisablePreviewMode();
  51. }
  52. }
  53. public void OnEnterRotateBlock(InputValue value)
  54. {
  55. float valueAsFloat = value.Get<float>();
  56. Debug.Log(valueAsFloat);
  57. _blockRotating = true;
  58. }
  59. public void OnExitRotateBlock(InputValue value)
  60. {
  61. float valueAsFloat = value.Get<float>();
  62. Debug.Log(valueAsFloat);
  63. _blockRotating = false;
  64. }
  65. public void OnRotateBlock(InputValue value)
  66. {
  67. if (_isPreview && _blockRotating)
  68. {
  69. Vector2 iMouseMovement = value.Get<Vector2>();
  70. Transform vTransform = _blockInstance.transform;
  71. //vTransform.Rotate(iMouseMovement.y / 10, iMouseMovement.x / 10, 0);
  72. vTransform.RotateAround(vTransform.position, transform.right, iMouseMovement.y / 10.0f);
  73. vTransform.RotateAround(vTransform.position, transform.up, iMouseMovement.x / 10.0f);
  74. //vTransform.eulerAngles += vTransform.worldToLocalMatrix.MultiplyVector(new Vector3(iMouseMovement.y / 10, iMouseMovement.x / 10, 0));
  75. //vTransform.Rotate(iMouseMovement.y/10, iMouseMovement.x/10, 0);
  76. _lastRotationState = vTransform.rotation;
  77. }
  78. }
  79. private void EnablePreviewMode()
  80. {
  81. if (_blockToPlace == null) return;
  82. Vector3 spawnPos = Vector3.forward * spawnDistance;
  83. _blockInstance = Instantiate(_blockToPlace, spawnPos, Quaternion.identity);
  84. _blockInstance.transform.SetParent(this.transform, false);
  85. _blockInstance.transform.rotation = _lastRotationState;
  86. _isPreview = true;
  87. SwitchToMeshRender();
  88. }
  89. private void DisablePreviewMode()
  90. {
  91. if (_blockInstance != null) Destroy(_blockInstance);
  92. _isPreview = false;
  93. }
  94. private void SwitchToMeshRender()
  95. {
  96. MeshRenderer vRenderer = _blockInstance.GetComponent<MeshRenderer>();
  97. Material vMaterial = vRenderer.material;
  98. _initialShader = vMaterial.shader;
  99. vMaterial.shader = previewShader;
  100. }
  101. private void SwitchToDefaultRender()
  102. {
  103. MeshRenderer vRenderer = _blockInstance.GetComponent<MeshRenderer>();
  104. Material vMaterial = vRenderer.material;
  105. vMaterial.shader = _initialShader;
  106. }
  107. }