Advertisement
evelynshilosky

PlacementSystem - Part 5

Feb 25th, 2025
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.82 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class PlacementSystem : MonoBehaviour
  4. {
  5.    /* public float placementDistance = 5f;
  6.     public LayerMask placementLayerMask;
  7.     public Material validPlacementMaterial;
  8.     public Material invalidPlacementMaterial;
  9.  
  10.     private GameObject placementIndicator;
  11.     private bool canPlace = false;
  12.     private Vector3 placementPosition;
  13.     private Quaternion placementRotation;
  14.     private bool isRotating = false;
  15.  
  16.     void Start()
  17.     {
  18.         placementIndicator = GameObject.CreatePrimitive(PrimitiveType.Cube);
  19.         placementIndicator.GetComponent<Collider>().enabled = false;
  20.         placementIndicator.GetComponent<Renderer>().material = validPlacementMaterial;
  21.         placementIndicator.SetActive(false);
  22.     }
  23.  
  24.     void Update()
  25.     {
  26.         if (InteractionSystem.Instance.IsPrecisionDropEnabled())
  27.         {
  28.             UpdatePlacementIndicator();
  29.             HandleRotation();
  30.         }
  31.         else
  32.         {
  33.             placementIndicator.SetActive(false);
  34.         }
  35.     }
  36.  
  37.     void UpdatePlacementIndicator()
  38.     {
  39.         RaycastHit hit;
  40.         if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, placementDistance, placementLayerMask))
  41.         {
  42.             placementPosition = hit.point + hit.normal * 0.05f;
  43.             placementRotation = Quaternion.LookRotation(hit.normal, Vector3.up);
  44.  
  45.             placementIndicator.transform.position = placementPosition;
  46.             placementIndicator.transform.rotation = placementRotation;
  47.             placementIndicator.SetActive(true);
  48.  
  49.             // Check if the placement is valid
  50.             Collider[] colliders = Physics.OverlapBox(placementPosition, placementIndicator.transform.localScale / 2, placementRotation, placementLayerMask);
  51.             canPlace = colliders.Length == 0;
  52.  
  53.             placementIndicator.GetComponent<Renderer>().material = canPlace ? validPlacementMaterial : invalidPlacementMaterial;
  54.         }
  55.         else
  56.         {
  57.             placementIndicator.SetActive(false);
  58.             canPlace = false;
  59.         }
  60.     }
  61.  
  62.     void HandleRotation()
  63.     {
  64.         if (Input.GetKeyDown(KeyCode.R))
  65.         {
  66.             isRotating = !isRotating;
  67.         }
  68.  
  69.         if (isRotating)
  70.         {
  71.             if (Input.GetKeyDown(KeyCode.Q))
  72.             {
  73.                 placementRotation *= Quaternion.Euler(0, -45, 0);
  74.             }
  75.             else if (Input.GetKeyDown(KeyCode.E))
  76.             {
  77.                 placementRotation *= Quaternion.Euler(0, 45, 0);
  78.             }
  79.         }
  80.     }
  81.  
  82.     public bool CanPlace()
  83.     {
  84.         return canPlace;
  85.     }
  86.  
  87.     public Vector3 GetPlacementPosition()
  88.     {
  89.         return placementPosition;
  90.     }
  91.  
  92.     public Quaternion GetPlacementRotation()
  93.     {
  94.         return placementRotation;
  95.     } */
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement