Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- public class E_Hover : MonoBehaviour {
- // The new script for handling the hover system in Diamond.
- // This is used on the base object and will ideally control all the core mechanics of the hover.
- // The main goal of this new script is to replace the old collision system with a new ray system.
- // 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.
- public Ray DetectionRay;
- public Vector3 RayPosition;
- public RaycastHit RayHit;
- public float RayDistance;
- public LayerMask DetectionLayers;
- public bool Horizontal;
- public GameObject ObjectHit;
- public GameObject ActiveSelectionBox;
- public int Size;
- int PreviousSize;
- // Stored values for how much we're going to offset our ray.
- public float SizeOffset;
- public GameObject[] SelectionBoxes;
- // Use this for initialization
- void Start () {
- }
- // Update is called once per frame
- void Update ()
- {
- foreach (GameObject I in SelectionBoxes)
- {
- 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.
- }
- if (PreviousSize != Size)
- {
- foreach(GameObject I in SelectionBoxes)
- {
- I.renderer.enabled = false;
- }
- PreviousSize = Size;
- // Set the active selection box based on our size.
- if (Size >= 1 && Size <= 5)
- ActiveSelectionBox = SelectionBoxes[Size - 1];
- else
- ActiveSelectionBox = SelectionBoxes[0];
- ActiveSelectionBox.renderer.enabled = true;
- // Couldn't quite figure out a clean way to get these values so I manually entered them.
- switch(Size)
- {
- case 1:
- SizeOffset = 6.5f;
- break;
- case 2:
- SizeOffset = 9.75f;
- break;
- case 3:
- SizeOffset = 13f;
- break;
- case 4:
- SizeOffset = 16.25f;
- break;
- case 5:
- SizeOffset = 19.5f;
- break;
- }
- }
- if (ActiveSelectionBox != null)
- {
- // First get the position of the active position.
- RayPosition = ActiveSelectionBox.transform.position;
- // 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.
- // 7 multiplied by the size of the cursor seems to be the magic number here.
- RayDistance = 7 * Size;
- // First adjust the visual cursor to the proper rotation.
- // Set the position of the cursor based on the offset (set above).
- // 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.
- // Set the direction the ray points at.
- if (Horizontal)
- {
- ActiveSelectionBox.transform.localEulerAngles = new Vector3(0, 180, 270);
- RayPosition.x -= SizeOffset + 0.25f;
- DetectionRay = new Ray(RayPosition, Vector3.right);
- }
- else
- {
- ActiveSelectionBox.transform.localEulerAngles = new Vector3(0, 180, -180);
- RayPosition.y += SizeOffset + 0.25f;
- DetectionRay = new Ray(RayPosition, Vector3.down);
- }
- // Detect the object being hit with the ray.
- if (Physics.Raycast(DetectionRay, out RayHit, RayDistance, DetectionLayers))
- {
- ObjectHit = RayHit.transform.gameObject;
- }
- else
- ObjectHit = null;
- // Displays a visual line for debugging the position of the ray in the editor.
- if (Horizontal)
- Debug.DrawLine(DetectionRay.origin, new Vector3(DetectionRay.origin.x + RayDistance, DetectionRay.origin.y, DetectionRay.origin.z));
- else
- Debug.DrawLine(DetectionRay.origin, new Vector3(DetectionRay.origin.x, DetectionRay.origin.y - RayDistance, DetectionRay.origin.z));
- }
- else
- {
- Debug.Log("Warning: Selection box is null.");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement