1 using System.Collections;
2 using System.Collections.Generic;
12 [RequireComponent(typeof(LineRenderer))]
16 public enum RayMode { STRAIGHT, PROJECTILE_MOTION }
28 public int proj_resolution = 3;
32 public float proj_max_verts = 150;
37 public float proj_velocity = 3;
41 public bool start_hidden =
true;
43 public float max_length = 25;
45 public float max_horizontal_distance = 10;
47 public float min_y_angle = 0;
50 protected LineRenderer
lr;
52 protected Vector3[] positions =
new Vector3[0];
53 protected Vector3 RayForward = Vector3.forward;
56 public bool isVisible {
57 get {
return !_hidden; }
61 public bool isHidden {
62 get {
return _hidden; }
72 if(!lr) lr = GetComponent<LineRenderer>();
74 AVR_DevConsole.error(
"AVR_Ray object "+gameObject.name+
" has no LineRenderer attatched!");
76 if(start_hidden) hide();
else show();
98 RayForward = transform.forward;
99 float ang = Mathf.Deg2Rad * min_y_angle;
100 if(RayForward.y > Mathf.Cos(ang)) {
102 RayForward = RayForward.normalized * Mathf.Sin(ang);
103 RayForward.y = Mathf.Cos(ang);
109 case RayMode.STRAIGHT: {
115 case RayMode.PROJECTILE_MOTION: {
116 UpdateProjectileRay();
120 AVR.Core.AVR_DevConsole.warn(
"RayMode type of AVR_Ray object "+gameObject.name+
" has not been implemented.");
129 if (positions.Length != 2) positions =
new Vector3[2];
132 Vector3 dest = RayForward * max_length;
133 if(
new Vector2(dest.x, dest.z).magnitude > max_horizontal_distance){
134 dest *= max_horizontal_distance / Mathf.Max(
new Vector2(dest.x, dest.z).magnitude, 0.0001f);
138 this.positions[0] = transform.position;
139 this.positions[1] = transform.position + dest;
142 lr.useWorldSpace =
true;
143 lr.positionCount = 2;
144 lr.SetPositions(positions);
149 List<Vector3> posl =
new List<Vector3>();
151 for (
int i = 0; i < proj_max_verts; i++)
153 float dist = (float)i / proj_resolution;
155 Vector3 dest = RayForward * dist;
158 posl.Add(transform.position + dest - Vector3.up * (dist * dist) / (proj_velocity * proj_velocity));
161 if (dist >= max_length)
break;
163 if (
new Vector2(dest.x, dest.z).magnitude > max_horizontal_distance)
break;
166 this.positions = posl.ToArray();
167 lr.useWorldSpace =
true;
168 lr.positionCount = posl.Count;
169 lr.SetPositions(this.positions);
188 lr.enabled = !hidden;
193 public bool synchronizeHidden =
false;
195 [Unity.Netcode.ServerRpc(RequireOwnership =
false)]
196 private void syncServerRpc(InternalState state)
198 m_ReplicatedState.Value = state;
203 if (!synchronizeHidden)
return;
206 InternalState state =
new InternalState();
207 state.FromReference(
this);
211 m_ReplicatedState.Value.ApplyState(
this);
215 private readonly Unity.Netcode.NetworkVariable<InternalState> m_ReplicatedState =
new Unity.Netcode.NetworkVariable<InternalState>(Unity.Netcode.NetworkVariableReadPermission.Everyone,
new InternalState());
217 private struct InternalState : IInternalState<AVR_Ray>
221 public void FromReference(AVR_Ray reference)
223 Hidden = reference.isHidden;
226 public void ApplyState(AVR_Ray reference)
228 reference.setHidden(Hidden);
231 public void NetworkSerialize<T>(Unity.Netcode.BufferSerializer<T> serializer) where T : Unity.Netcode.IReaderWriter
233 serializer.SerializeValue(ref Hidden);
Base class for a ray. A ray is always cast in transform.forward direction of the object it is attatch...
Makes a property of an object only show in the Network-behaviour window. Also works for private/prote...
virtual void UpdateRay()
Updates the ray. Called from Monobehaviour.Update()
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
virtual void UpdateStraightRay()
Updates a ray with mode==STRAIGHT
virtual void setHidden(bool hidden)
Set the hidden status of the ray
virtual void UpdateProjectileRay()
Updates a ray with mode==PROJECTILE
Allows for simple hiding of properties in the UnityEditor depending on certain conditions. For instance, in the following example the "type" field will only be displayed in the inspector if the "tracking" field is set to true:
virtual void hide()
Hides the ray. A ray is not updated while hidden.
Base class for all arc-vr components. A component is typically a Monobehaviour that represents a virt...
virtual void show()
Shows the ray. A ray is not updated while hidden.