BuildingGhost2D.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BuildingGhost2D : MonoBehaviour {
  5. private Transform visual;
  6. private PlacedObjectTypeSO placedObjectTypeSO;
  7. private void Start() {
  8. RefreshVisual();
  9. GridBuildingSystem2D.Instance.OnSelectedChanged += Instance_OnSelectedChanged;
  10. }
  11. private void Instance_OnSelectedChanged(object sender, System.EventArgs e) {
  12. RefreshVisual();
  13. }
  14. private void LateUpdate() {
  15. Vector3 targetPosition = GridBuildingSystem2D.Instance.GetMouseWorldSnappedPosition();
  16. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 15f);
  17. transform.rotation = Quaternion.Lerp(transform.rotation, GridBuildingSystem2D.Instance.GetPlacedObjectRotation(), Time.deltaTime * 15f);
  18. }
  19. private void RefreshVisual() {
  20. if (visual != null) {
  21. Destroy(visual.gameObject);
  22. visual = null;
  23. }
  24. PlacedObjectTypeSO placedObjectTypeSO = GridBuildingSystem2D.Instance.GetPlacedObjectTypeSO();
  25. if (placedObjectTypeSO != null) {
  26. visual = Instantiate(placedObjectTypeSO.visual, Vector3.zero, Quaternion.identity);
  27. visual.parent = transform;
  28. visual.localPosition = Vector3.zero;
  29. visual.localEulerAngles = Vector3.zero;
  30. }
  31. }
  32. }