Advertisement
evelynshilosky

PlacebleItem - Part 33

Mar 7th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3.  
  4. public class PlacebleItem : MonoBehaviour
  5. {
  6.     // Validation
  7.     [SerializeField] bool isGrounded;
  8.     [SerializeField] bool isOverlappingItems;
  9.     public bool isValidToBeBuilt;
  10.  
  11.     [SerializeField] BoxCollider solidCollider;
  12.     private Outline outline;
  13.  
  14.     private void Start()
  15.     {
  16.         outline = GetComponent<Outline>();
  17.     }
  18.  
  19.     void Update()
  20.     {
  21.         if (isGrounded && isOverlappingItems == false)
  22.         {
  23.             isValidToBeBuilt = true;
  24.         }
  25.         else
  26.         {
  27.             isValidToBeBuilt = false;
  28.         }
  29.  
  30.         // Raycast from the box's position towards its center
  31.  
  32.         var boxHeight = transform.lossyScale.y;
  33.         RaycastHit groundHit;
  34.         if (Physics.Raycast(transform.position, Vector3.down, out groundHit, boxHeight * 0.5f, LayerMask.GetMask("Ground")))
  35.         {
  36.             isGrounded = true;
  37.         }
  38.         else
  39.         {
  40.             isGrounded = false;
  41.         }
  42.  
  43.     }
  44.  
  45.     #region || --- On Triggers --- |
  46.     private void OnTriggerEnter(Collider other)
  47.     {
  48.         if (other.CompareTag("Ground") && PlacementSystem.Instance.inPlacementMode)
  49.         {
  50.             // Making sure the item is parallel to the ground
  51.             RaycastHit hit;
  52.             if (Physics.Raycast(transform.position, Vector3.down, out hit, Mathf.Infinity, LayerMask.GetMask("Ground")))
  53.             {
  54.                 // Align the box's rotation with the ground normal
  55.                 Quaternion newRotation = Quaternion.FromToRotation(transform.up, hit.normal) * transform.rotation;
  56.                 transform.rotation = newRotation;
  57.  
  58.                 isGrounded = true;
  59.             }
  60.         }
  61.  
  62.         if (other.CompareTag("Tree") || other.CompareTag("pickable"))
  63.         {
  64.             isOverlappingItems = true;
  65.         }
  66.     }
  67.     #endregion
  68.  
  69.     private void OnTriggerExit(Collider other)
  70.     {
  71.         if (other.CompareTag("Ground") && PlacementSystem.Instance.inPlacementMode)
  72.         {
  73.             isGrounded = false;
  74.         }
  75.  
  76.         if (other.CompareTag("Tree") || other.CompareTag("pickable") && PlacementSystem.Instance.inPlacementMode)
  77.         {
  78.             isOverlappingItems = false;
  79.         }
  80.     }
  81.  
  82.     #region || --- Set Outline Colors --- |
  83.     public void SetInvalidColor()
  84.     {
  85.         if (outline != null)
  86.         {
  87.             outline.enabled = true;
  88.             outline.OutlineColor = Color.red;
  89.         }
  90.  
  91.     }
  92.  
  93.     public void SetValidColor()
  94.     {
  95.         if (outline != null)
  96.         {
  97.             outline.enabled = true;
  98.             outline.OutlineColor = Color.green;
  99.         }
  100.     }
  101.  
  102.     public void SetDefaultColor()
  103.     {
  104.         if (outline != null)
  105.         {
  106.             outline.enabled = false;
  107.         }
  108.     }
  109.     #endregion
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement