Advertisement
CassataGames

Editor Cursor Script

Dec 6th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class E_Hover : MonoBehaviour {
  5.  
  6.     // The new script for handling the hover system in Diamond.
  7.     // This is used on the base object and will ideally control all the core mechanics of the hover.
  8.     // The main goal of this new script is to replace the old collision system with a new ray system.
  9.     // A single ray of varying lengths will be used to 'see' if an object is in the way of where an object is to be placed.
  10.  
  11.     public Ray DetectionRay;
  12.     public Vector3 RayPosition;
  13.     public RaycastHit RayHit;
  14.     public float RayDistance;
  15.     public LayerMask DetectionLayers;
  16.     public bool Horizontal;
  17.     public GameObject ObjectHit;
  18.  
  19.     public GameObject ActiveSelectionBox;
  20.  
  21.     public int Size;
  22.     int PreviousSize;
  23.     // Stored values for how much we're going to offset our ray.
  24.     public float SizeOffset;
  25.     public GameObject[] SelectionBoxes;
  26.  
  27.     // Use this for initialization
  28.     void Start () {
  29.    
  30.     }
  31.    
  32.     // Update is called once per frame
  33.     void Update ()
  34.     {
  35.         foreach (GameObject I in SelectionBoxes)
  36.         {
  37.             I.transform.position = new Vector3(EditorGlobals.Access.GridLocation.x, EditorGlobals.Access.GridLocation.y, EditorGlobals.Access.GridLocation.z); // Colliders DO NOT get offset on the Z axis.
  38.         }
  39.  
  40.         if (PreviousSize != Size)
  41.         {
  42.             foreach(GameObject I in SelectionBoxes)
  43.             {
  44.                 I.renderer.enabled = false;
  45.             }
  46.             PreviousSize = Size;
  47.             // Set the active selection box based on our size.
  48.             if (Size >= 1 && Size <= 5)
  49.                 ActiveSelectionBox = SelectionBoxes[Size - 1];
  50.             else
  51.                 ActiveSelectionBox = SelectionBoxes[0];
  52.             ActiveSelectionBox.renderer.enabled = true;
  53.  
  54.             // Couldn't quite figure out a clean way to get these values so I manually entered them.
  55.             switch(Size)
  56.             {
  57.                 case 1:
  58.                     SizeOffset = 6.5f;
  59.                     break;
  60.                 case 2:
  61.                     SizeOffset = 9.75f;
  62.                     break;
  63.                 case 3:
  64.                     SizeOffset = 13f;
  65.                     break;
  66.                 case 4:
  67.                     SizeOffset = 16.25f;
  68.                     break;
  69.                 case 5:
  70.                     SizeOffset = 19.5f;
  71.                     break;
  72.             }
  73.         }
  74.  
  75.         if (ActiveSelectionBox != null)
  76.         {
  77.             // First get the position of the active position.
  78.             RayPosition = ActiveSelectionBox.transform.position;
  79.             // The ray should be slightly larger than a single cell (6.5) so that it can detect if you're half way over an object.
  80.             // 7 multiplied by the size of the cursor seems to be the magic number here.
  81.             RayDistance = 7 * Size;
  82.  
  83.             // First adjust the visual cursor to the proper rotation.
  84.             // Set the position of the cursor based on the offset (set above).
  85.             // The additional 25% is because Unity doesn't detect collisions inside the origin of a ray. This added overhang allows the ray to originate outside the target cell.
  86.             // Set the direction the ray points at.
  87.             if (Horizontal)
  88.             {
  89.                 ActiveSelectionBox.transform.localEulerAngles = new Vector3(0, 180, 270);
  90.                 RayPosition.x -= SizeOffset + 0.25f;
  91.                 DetectionRay = new Ray(RayPosition, Vector3.right);
  92.             }
  93.             else
  94.             {
  95.                 ActiveSelectionBox.transform.localEulerAngles = new Vector3(0, 180, -180);
  96.                 RayPosition.y += SizeOffset + 0.25f;
  97.                 DetectionRay = new Ray(RayPosition, Vector3.down);
  98.             }
  99.             // Detect the object being hit with the ray.
  100.             if (Physics.Raycast(DetectionRay, out RayHit, RayDistance, DetectionLayers))
  101.             {
  102.                 ObjectHit = RayHit.transform.gameObject;
  103.             }
  104.             else
  105.                 ObjectHit = null;
  106.  
  107.             // Displays a visual line for debugging the position of the ray in the editor.
  108.             if (Horizontal)
  109.                 Debug.DrawLine(DetectionRay.origin, new Vector3(DetectionRay.origin.x + RayDistance, DetectionRay.origin.y, DetectionRay.origin.z));
  110.             else
  111.                 Debug.DrawLine(DetectionRay.origin, new Vector3(DetectionRay.origin.x, DetectionRay.origin.y - RayDistance, DetectionRay.origin.z));
  112.         }
  113.         else
  114.         {
  115.             Debug.Log("Warning: Selection box is null.");
  116.         }          
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement