VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_SolidRay.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 
5 namespace AVR.Core {
6  /// <summary>
7  /// A more advanced type of AVR_Ray. The ray can be used to get the first object hit by it.
8  /// To limit collidable objects by certain types, use AVR_SolidRay.passThrough_layerMask.
9  /// </summary>
10  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_core_1_1_a_v_r___solid_ray.html")]
11  [RequireComponent(typeof(LineRenderer))]
12  public class AVR_SolidRay : AVR_Ray
13  {
14  [Header("Physics")]
15  //// MEMBERS ===================================================================================================
16  /// <summary> Layermask used to selectively ignore colliders when casting a ray. </summary>
17  public LayerMask hit_layerMask;
18 
19  /// <summary>
20  /// Raycasthit where the ray hit something.
21  /// Contains the last hit the ray did if AVR_SolidRay.objectHit is false.
22  /// null if the ray hasn't hit a single object in its lifetime.
23  ///</summary>
24  public RaycastHit hitPosition
25  {
26  get { return _hitPosition; }
27  }
28  private RaycastHit _hitPosition;
29 
30  /// <summary> True if the ray hit some object this frame. Use AVR_SolidRay.hitPosition to get the respective RaycastHit. </summary>
31  public bool objectHit
32  {
33  get { return _objectHit; }
34  }
35  private bool _objectHit;
36 
37 
38  //// METHODS ===================================================================================================
39 
40  protected override void UpdateRay()
41  {
42  _objectHit = false;
43  base.UpdateRay();
44  }
45 
46  // Raycasting functionality for straight rays:
47  protected override void UpdateStraightRay()
48  {
49  base.UpdateStraightRay();
50  // If we hit something along the way, cut beam off there
51  checkHit(positions);
52  }
53 
54  // Raycasting functionality for projectile rays:
55  // NOTE: We're rewriting the base function from AVR_Ray at this point so we can calculate collisions and positions side-by-side and not back-to-back
56  protected override void UpdateProjectileRay()
57  {
58  List<Vector3> posl = new List<Vector3>();
59 
60  for (int i = 0; i < this.proj_max_verts; i++)
61  {
62  float dist = (float)i / this.proj_resolution;
63 
64  Vector3 dest = RayForward * dist;
65 
66  // Add new vertex to line
67  posl.Add(transform.position + dest - Vector3.up * (dist * dist) / (proj_velocity * proj_velocity));
68 
69  // If we have 2 or more vertices check for collisions
70  if(checkHit(posl)) break;
71 
72  // Check if we're within distance limitations. NOTE: We are only restricting distance in the direction of RayForward, not up or down.
73  if (dist >= max_length) break;
74 
75  // Deal with max_horizontal_distance
76  if (new Vector2(dest.x, dest.z).magnitude > max_horizontal_distance) {
77  // We merely Linecast 100 units downward.
78  posl.Add(posl[posl.Count-1] + Vector3.down*100);
79  checkHit(posl);
80  break;
81  }
82  }
83 
84  this.positions = posl.ToArray();
85  lr.useWorldSpace = true;
86  lr.positionCount = posl.Count;
87  lr.SetPositions(this.positions);
88  }
89 
90  // Checks if there is a hit between the last 2 positions.
91  private bool checkHit(List<Vector3> positions) {
92  if (positions.Count > 1 && AVR.Core.Utils.Phys.LineCast(positions[positions.Count - 2], positions[positions.Count - 1], out RaycastHit hit, hit_layerMask))
93  {
94  _objectHit = true;
95  _hitPosition = hit;
96  positions[positions.Count-1] = _hitPosition.point;
97  return true;
98  }
99  return false;
100  }
101 
102  // Checks if there is a hit between the last 2 positions.
103  private bool checkHit(Vector3[] positions)
104  {
105  if (positions.Length > 1 && AVR.Core.Utils.Phys.LineCast(positions[positions.Length - 2], positions[positions.Length - 1], out RaycastHit hit, hit_layerMask))
106  {
107  _objectHit = true;
108  _hitPosition = hit;
109  positions[positions.Length - 1] = _hitPosition.point;
110  return true;
111  }
112  return false;
113  }
114  }
115 }
override void UpdateRay()
Updates the ray. Called from Monobehaviour.Update()
Definition: AVR_SolidRay.cs:40
LayerMask hit_layerMask
Layermask used to selectively ignore colliders when casting a ray.
Definition: AVR_SolidRay.cs:17
Base class for a ray. A ray is always cast in transform.forward direction of the object it is attatch...
Definition: AVR_Ray.cs:13
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
RaycastHit _hitPosition
Definition: AVR_SolidRay.cs:28
override void UpdateStraightRay()
Updates a ray with mode==STRAIGHT
Definition: AVR_SolidRay.cs:47
A more advanced type of AVR_Ray. The ray can be used to get the first object hit by it...
Definition: AVR_SolidRay.cs:12
override void UpdateProjectileRay()
Updates a ray with mode==PROJECTILE
Definition: AVR_SolidRay.cs:56
bool checkHit(Vector3[] positions)
bool checkHit(List< Vector3 > positions)
Definition: AVR_SolidRay.cs:91