BuildingGhost.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BuildingGhost : MonoBehaviour {
  5. private Transform visual;
  6. private PlacedObjectTypeSO placedObjectTypeSO;
  7. private void Start() {
  8. RefreshVisual();
  9. CMGridBuildingSystem3D.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 = CMGridBuildingSystem3D.Instance.GetMouseWorldSnappedPosition();
  16. targetPosition.y = 1f;
  17. transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * 15f);
  18. transform.rotation = Quaternion.Lerp(transform.rotation, CMGridBuildingSystem3D.Instance.GetPlacedObjectRotation(), Time.deltaTime * 15f);
  19. }
  20. private void RefreshVisual() {
  21. if (visual != null) {
  22. Destroy(visual.gameObject);
  23. visual = null;
  24. }
  25. PlacedObjectTypeSO placedObjectTypeSO = CMGridBuildingSystem3D.Instance.GetPlacedObjectTypeSO();
  26. if (placedObjectTypeSO != null) {
  27. visual = Instantiate(placedObjectTypeSO.visual, Vector3.zero, Quaternion.identity);
  28. visual.parent = transform;
  29. visual.localPosition = Vector3.zero;
  30. visual.localEulerAngles = Vector3.zero;
  31. SetLayerRecursive(visual.gameObject, 11);
  32. }
  33. }
  34. private void SetLayerRecursive(GameObject targetGameObject, int layer) {
  35. targetGameObject.layer = layer;
  36. foreach (Transform child in targetGameObject.transform) {
  37. SetLayerRecursive(child.gameObject, layer);
  38. }
  39. }
  40. }