1 using System.Collections;
2 using System.Collections.Generic;
17 public enum DirectionType { RELATIVE_TO_CONTROLLER, RELATIVE_TO_HEADSET, RELATIVE_TO_RIG, ABSOLUTE }
37 [Header(
"Direction Controls")]
54 public float speed = 2.0f;
59 public bool use_gravity =
true;
64 public bool disable_y_axis =
false;
69 public bool use_acceleration =
false;
76 public float acceleration_constant = 0.1f;
79 private System.Func<Vector2, Vector3>
tF;
80 private CharacterController ch => playerRig.characterController;
81 private Vector3 movement = Vector3.zero;
88 case DirectionAxes.XY: { tF = (v) =>
new Vector3(v.x, v.y, 0);
break; }
89 case DirectionAxes.XZ: { tF = (v) =>
new Vector3(v.x, 0, v.y);
break; }
90 case DirectionAxes.YX: { tF = (v) =>
new Vector3(v.y, v.x, 0);
break; }
91 case DirectionAxes.YZ: { tF = (v) =>
new Vector3(0, v.x, v.y);
break; }
92 case DirectionAxes.ZX: { tF = (v) =>
new Vector3(v.y, 0, v.x);
break; }
93 case DirectionAxes.ZY: { tF = (v) =>
new Vector3(0, v.y, v.x);
break; }
94 default: { AVR_DevConsole.cwarn(
"The given DirectionAxes are invalid.",
this); tF = (v) =>
new Vector3(v.x, v.y, 0);
break; }
97 if(!use_acceleration) acceleration_constant = 1.0f;
107 if (!IsOwner)
return;
111 if (controller.inputManager.getEventStatus(moveEvent)) {
112 Vector2 md = controller.inputManager.getEventStatus(moveDirection);
113 Vector3 dir = tF(md);
116 case DirectionType.RELATIVE_TO_CONTROLLER : {
117 dir = controller.transform.TransformDirection(dir);
120 case DirectionType.RELATIVE_TO_RIG : {
121 dir = playerRig.transform.TransformDirection(dir);
124 case DirectionType.RELATIVE_TO_HEADSET : {
125 dir = playerRig.MainCamera.transform.TransformDirection(dir);
130 movement = Vector3.Lerp(movement, dir * speed, acceleration_constant);
132 if(disable_y_axis) movement.y = 0;
135 ch.Move(movement * Time.deltaTime);
138 Vector3 gravity = Physics.gravity * Time.deltaTime;
143 AVR_DevConsole.cerror(
"The PlayerRig does not have a charactercontroller. LocomotionProvider requires a charactercontroller.",
this);
151 public bool synchronizeCharacterController =
false;
153 [Unity.Netcode.ServerRpc(RequireOwnership =
false)]
154 private void syncServerRpc(InternalState state)
156 m_ReplicatedState.Value = state;
161 if (!synchronizeCharacterController)
return;
164 InternalState state =
new InternalState();
165 state.FromReference(
this);
169 m_ReplicatedState.Value.ApplyState(
this);
173 private readonly Unity.Netcode.NetworkVariable<InternalState> m_ReplicatedState =
new Unity.Netcode.NetworkVariable<InternalState>(Unity.Netcode.NetworkVariableReadPermission.Everyone,
new InternalState());
175 private struct InternalState : IInternalState<AVR_LocomotionProvider>
177 public Vector3 chCenter;
178 public float chHeight;
180 public void FromReference(AVR_LocomotionProvider reference)
182 chCenter = reference.ch.center;
183 chHeight = reference.ch.height;
186 public void ApplyState(AVR_LocomotionProvider reference)
188 reference.ch.center = chCenter;
189 reference.ch.height = chHeight;
192 public void NetworkSerialize<T>(Unity.Netcode.BufferSerializer<T> serializer) where T : Unity.Netcode.IReaderWriter
194 serializer.SerializeValue(ref chCenter);
195 serializer.SerializeValue(ref chHeight);
AVR_ControllerInputManager.Vec2Event moveDirection
The event we retrieve our movement vector from.
Makes a property of an object only show in the Network-behaviour window. Also works for private/prote...
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
Provides locomotion-movement to a controller.
DirectionAxes
The input is given by a 2d vector from the controller (moveDirection), this value determines how this...
AVR_Component specifically attatched to an AVR_Controller. Needs to have an AVR_Controller on this ga...
DirectionType
What should determine the direction of movement? Example: "RELATIVE_TO_CONTROLLER" will move the Rig ...
AVR_ControllerInputManager.BoolEvent moveEvent
Movement is only performed when this event is true.
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:
System.Func< Vector2, Vector3 > tF