Advertisement
Guest User

collisionBox

a guest
Apr 7th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using EasyBuildSystem.Runtimes.Internal.Builder;
  2. using EasyBuildSystem.Runtimes.Internal.Managers;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using UnityEngine;
  7.  
  8. public class CollisionDetection : MonoBehaviour
  9. {
  10.     public GameObject m_MyObject;
  11.     BoxCollider m_Collider;
  12.  
  13.     void Start()
  14.     {
  15.         if (m_MyObject != null)
  16.             m_Collider = m_MyObject.GetComponent<BoxCollider>();
  17.     }
  18.  
  19.     private void Update()
  20.     {
  21.         if (GameMaster.Instance.BuildSystem.currentPreview != null)
  22.         {
  23.             m_Collider.center = GameMaster.Instance.BuildSystem.currentPreview.GetComponent<BoxCollider>().center;
  24.             m_Collider.size = GameMaster.Instance.BuildSystem.currentPreview.GetComponent<BoxCollider>().size;
  25.         }
  26.  
  27.         Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
  28.  
  29.         RaycastHit[] hitpoints = Physics.RaycastAll(ray).OrderBy(h => h.distance).ToArray(); //sort by distance
  30.         foreach (RaycastHit hit in hitpoints)
  31.         {
  32.             if (hit.collider.gameObject != this.gameObject)
  33.             {
  34.                 Vector3 newPos = hit.point;
  35.                 transform.position = newPos;
  36.                 continue;
  37.             }
  38.         }
  39.  
  40.         if (GameMaster.Instance.placedParts != null)
  41.         {
  42.             var allowPlacement = true;
  43.  
  44.             foreach (var part in GameMaster.Instance.placedParts)
  45.             {
  46.                 if (m_Collider.bounds.Intersects(part.GetComponent<BoxCollider>().bounds))
  47.                 {
  48.                     allowPlacement = false;
  49.                     break; ;
  50.                 }
  51.             }
  52.  
  53.             if (!allowPlacement)
  54.             {
  55.                 GameMaster.Instance.BuildSystem.allowPlacement = false;
  56.             }
  57.             else
  58.             {
  59.                 GameMaster.Instance.BuildSystem.allowPlacement = true;
  60.             }
  61.         }  
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement