Guest User

Collisions

a guest
Apr 2nd, 2020
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.39 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5.  
  6. public class CollisionDetection : MonoBehaviour
  7. {
  8.     private void Update()
  9.     {
  10.             if (gameObject.layer == 2)
  11.         {
  12.             Ray ray = Camera.main.ScreenPointToRay(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1.0f));
  13.  
  14.             RaycastHit[] hitpoints = Physics.RaycastAll(ray).OrderBy(h => h.distance).ToArray(); //sort by distance
  15.             foreach (RaycastHit hit in hitpoints)
  16.             {
  17.                 if (hit.collider.gameObject != this.gameObject)
  18.                 {
  19.                     Vector3 newPos = hit.point;
  20.                     transform.position = newPos;
  21.                     break; //stop iterating
  22.                 }
  23.             }
  24.         }
  25.     }
  26.  
  27.  
  28.     private void OnTriggerEnter(Collider other)
  29.     {
  30.         if (other.gameObject.tag == "Untagged")
  31.         {
  32.             Debug.Log("<i>detected</i> " + other.gameObject.name);
  33.             GameMaster.Instance.isCollided = true;
  34.         }
  35.     }
  36.  
  37.     private void OnTriggerExit(Collider other)
  38.     {
  39.         if (other.gameObject.tag == "Untagged")
  40.         {
  41.             Debug.Log("<i>OUT </i>" + other.gameObject.name);
  42.             GameMaster.Instance.isCollided = false;
  43.         }
  44.     }
  45.  
  46.     private void OnCollisionEnter(Collision collision)
  47.     {
  48.        
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment