Advertisement
Guest User

CollisionDetection

a guest
Apr 5th, 2020
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 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.                 break;
  37.             }
  38.         }
  39.  
  40.         if (GameMaster.Instance.placedParts != null)
  41.         {
  42.             foreach (var part in GameMaster.Instance.placedParts)
  43.             {
  44.                 Debug.Log(GameMaster.Instance.placedParts.Count);
  45.  
  46.                 if (m_Collider.bounds.Intersects(part.GetComponent<BoxCollider>().bounds))
  47.                 {
  48.                     GameMaster.Instance.BuildSystem.allowPlacement = false;
  49.                 }
  50.                 else
  51.                 {
  52.                     GameMaster.Instance.BuildSystem.allowPlacement = true;
  53.                 }
  54.             }
  55.         }
  56.        
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement