1 using System.Collections;
2 using System.Collections.Generic;
16 private Vector3 _RigPosInWorldSpace =>
18 !IsOwner? m_ReplicatedState.Value.rigPos :
20 this.transform.position;
22 private Quaternion _RigRotInWorldSpace =>
24 !IsOwner ? m_ReplicatedState.Value.rigRot :
26 this.transform.rotation;
28 private Vector3 _CameraPosInWorldSpace =>
30 !IsOwner ? m_ReplicatedState.Value.camPos :
32 this.MainCamera.transform.position;
34 private Quaternion _CameraRotInWorldSpace =>
36 !IsOwner ? m_ReplicatedState.Value.camRot :
38 this.MainCamera.transform.rotation;
43 public Vector3 AvgMotion {
44 get {
return _motion; }
46 private Vector3 _motion = Vector3.zero;
57 public CharacterController characterController {
58 get {
return _characterController; }
66 public Vector3 RigInWorldSpace {
67 get {
return _RigPosInWorldSpace; }
73 public Quaternion RigRotationInWorldSpace
75 get {
return _RigRotInWorldSpace; }
81 public Vector3 CameraInWorldSpace {
82 get {
return _CameraPosInWorldSpace; }
88 public Quaternion CameraRotationInWorldSpace
90 get {
return _CameraRotInWorldSpace; }
96 public Vector3 LocalViewVector {
97 get {
return MainCamera.transform.InverseTransformDirection(MainCamera.transform.forward); }
103 public Vector3 ViewVector {
104 get {
return MainCamera.transform.forward; }
110 public Vector3 ViewPos
112 get {
return MainCamera.transform.position + MainCamera.transform.forward; }
119 public Vector3 XZPlaneFacingDirection {
120 get { Vector3 tf = Vector3.Cross(MainCamera.transform.right, Vector3.up); tf.y = 0;
return tf; }
126 public Vector3 CameraInRigSpace {
127 get {
return transform.InverseTransformPoint(MainCamera.transform.position); }
133 public float CameraHeightInRigSpace {
134 get {
return CameraInWorldSpace.y - RigInWorldSpace.y; }
140 public Vector3 FeetInWorldSpace {
141 get {
return CameraInWorldSpace - transform.up*CameraHeightInRigSpace; }
147 public Vector3 FeetInRigSpace {
148 get {
return transform.InverseTransformPoint(FeetInWorldSpace); }
155 transform.position = (pos + (RigInWorldSpace - FeetInWorldSpace));
156 if(!affectMotion) _lastFPos = pos;
163 transform.position = pos;
164 if(!affectMotion) _lastFPos = FeetInWorldSpace;
171 transform.position += (pos - CameraInWorldSpace);
172 if(!affectMotion) _lastFPos = FeetInWorldSpace;
180 float val1 = CameraHeightInRigSpace;
181 float hf = AVR_Settings.get_float(
"/settings/calibration/player_height");
183 if (leftHandController && rightHandController)
185 float val2 = 0.5f * ((leftHandController.transform.position.y - transform.position.y) + (rightHandController.transform.position.y - transform.position.y));
186 return val1 < 0.72f * hf || val2 < 0.36f * hf;
196 return MainCamera.transform.rotation.eulerAngles.x > 55.0f && MainCamera.transform.rotation.eulerAngles.x < 120.0f;
204 return Mathf.Clamp((MainCamera.transform.rotation.eulerAngles.x - 40.0f) / 30.0f, 0.0f, 1.0f);
220 _characterController = GetComponent<CharacterController>();
221 if (characterController)
223 AVR_DevConsole.print(gameObject.name +
" got CharacterController.");
226 if(!MainCamera) MainCamera = Camera.main;
227 if(!MainCamera) AVR_DevConsole.cerror(
"The MainCamera property is not set and there is no Camera.main component in the scene!",
this);
231 Vector3.SmoothDamp(_motion, (FeetInWorldSpace - _lastFPos) / Time.deltaTime, ref _motion, 0.5f);
232 _lastFPos = FeetInWorldSpace;
236 if(characterController) {
238 characterController.height = Mathf.Clamp(AVR_PlayerRig.Instance.CameraHeightInRigSpace, AVR_Settings.get_float(
"/settings/core/charactercontroller/min_height"),
AVR_Settings.
get_float(
"/settings/core/charactercontroller/max_height"));
242 Vector3 center = CameraInRigSpace;
243 center.y = characterController.skinWidth + (characterController.height * 0.5f);
244 characterController.center = center;
251 public bool synchronizeRigPositions =
false;
253 [ServerRpc(RequireOwnership =
false)]
254 private void syncServerRpc(InternalState state)
256 m_ReplicatedState.Value = state;
263 InternalState state =
new InternalState();
264 state.FromReference(
this);
268 m_ReplicatedState.Value.ApplyState(
this);
272 private readonly NetworkVariable<InternalState> m_ReplicatedState =
new NetworkVariable<InternalState>(NetworkVariableReadPermission.Everyone,
new InternalState());
274 private struct InternalState : IInternalState<AVR_PlayerRig>
276 public Vector3 rigPos;
277 public Quaternion rigRot;
278 public Vector3 camPos;
279 public Quaternion camRot;
281 public void FromReference(AVR_PlayerRig reference)
283 rigPos = reference.RigInWorldSpace;
284 rigRot = reference.RigRotationInWorldSpace;
285 camPos = reference.CameraInWorldSpace;
286 camRot = reference.CameraRotationInWorldSpace;
289 public void ApplyState(AVR_PlayerRig reference)
294 public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
296 serializer.SerializeValue(ref rigPos);
297 serializer.SerializeValue(ref rigRot);
298 serializer.SerializeValue(ref camPos);
299 serializer.SerializeValue(ref camRot);
CharacterController _characterController
void MoveRigToCameraPosition(Vector3 pos, bool affectMotion=true)
Instantly moves the PlayerRig so that the camera ends up at world coordinates 'pos' ...
Camera MainCamera
Camera that represents the HMD
Represents the players VR setup. Only one instance at a time is allowed.
Represents a VR controller. Provides functionality for spacial tracking and haptic feedback...
void MoveRigToPosition(Vector3 pos, bool affectMotion=true)
Instantly moves the PlayerRig to a given location
Contains constan values, settings etc. which can all be set through *.avr files. Duplicate settings w...
bool isCrouching()
Returns true if the player is crouching / crawling. Uses the "/settings/calibration/player_height" se...
bool isLeaningForwards()
Returns true if the player is leaning forwards / bowing.
Makes a property of an object only show in the Network-behaviour window. Also works for private/prote...
static float get_float(string token)
Get a registered setting of the type float.
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
float isLeaningForwardsConfidence()
Returns a confidence value of how much the player is leaning forwards / bowing.
void MoveRigToFeetPosition(Vector3 pos, bool affectMotion=true)
Instantly moves the PlayerRig so that the players feet (anchor) end up at world coordinates 'pos' ...