VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
AVR_TurnProvider.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using UnityEditor;
5 
6 using AVR;
7 using AVR.Core;
8 using AVR.Core.Attributes;
9 
10 namespace AVR.Motion {
11  [AVR.Core.Attributes.DocumentationUrl("class_a_v_r_1_1_motion_1_1_a_v_r___turn_provider.html")]
12  /// <summary>
13  /// Provides turning functionality to a controller.
14  /// </summary>
16  {
17  /// <summary>
18  /// Mode of turning. Snap will instantly turn by a given amount while smooth is a continous turn over time
19  /// </summary>
20  public enum turnMode { SNAP, SMOOTH };
21 
22  [Header("Mode")]
23  /// <summary>
24  /// Mode of turning. Snap will instantly turn by a given amount while smooth is a continous turn over time
25  /// </summary>
26  public turnMode mode;
27 
28  [Header("Input")]
29  /// <summary>
30  /// Event on which a turn is performed.
31  /// </summary>
33 
34  /// <summary>
35  /// Float value that determines the direction of the turn.
36  /// </summary>
38 
39  [Header("Settings")]
41  /// <summary>
42  /// If the mode is set to snap, the amount of rotation (in degrees) is determined by this value
43  /// </summary>
44  public float snp_rotationAmount = 45;
45 
47  /// <summary>
48  /// If the mode is set to snap, this determines the amount of time (in seconds) that has to pass between turns
49  /// </summary>
50  public float snp_cooldown = 0.5f;
51 
53  /// <summary>
54  /// If the mode is set to smooth, this determines the speed of rotation (id deg/s)
55  /// </summary>
56  public float smt_rotationSpeed = 45; //In degrees per second
57 
58  private float stime;
59 
60  protected override void Awake()
61  {
62  base.Awake();
63  stime = Time.time;
64  }
65 
66  void Update()
67  {
68 #if AVR_NET
69  if (IsOnline && !IsOwner) return;
70 #endif
71 
72  // Snap turn triggered
73  if(mode==turnMode.SNAP && controller.inputManager.getEventStatus(turnEvent) && (stime + snp_cooldown) < Time.time) {
74  stime = Time.time; // Restart cooldown timer
75  // Turn left
76  if (controller.inputManager.getEventStatus(turnDirection) < -0.5)
77  playerRig.transform.Rotate(Vector3.up, -snp_rotationAmount, Space.Self);
78  // Turn right
79  else if (controller.inputManager.getEventStatus(turnDirection) > 0.5)
80  playerRig.transform.Rotate(Vector3.up, snp_rotationAmount, Space.Self);
81  }
82 
83  else if(mode==turnMode.SMOOTH && controller.inputManager.getEventStatus(turnEvent)) {
84  // Turn left
85  if (controller.inputManager.getEventStatus(turnDirection) < -0.5)
86  playerRig.transform.Rotate(Vector3.up, -smt_rotationSpeed * Time.deltaTime, Space.Self);
87  // Turn right
88  else if (controller.inputManager.getEventStatus(turnDirection) > 0.5)
89  playerRig.transform.Rotate(Vector3.up, smt_rotationSpeed * Time.deltaTime, Space.Self);
90  }
91  }
92  }
93 }
BoolEvent
Boolean-returning events of a controller.
FloatEvent
Floating-Point-number-returning events of a controller.
Provides turning functionality to a controller.
turnMode
Mode of turning. Snap will instantly turn by a given amount while smooth is a continous turn over tim...
Sets the documentation html file inside of Packages/com.avr.core/Documentation/html of a given class...
AVR_ControllerInputManager.FloatEvent turnDirection
Float value that determines the direction of the turn.
AVR_Component specifically attatched to an AVR_Controller. Needs to have an AVR_Controller on this ga...
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:
AVR_ControllerInputManager.BoolEvent turnEvent
Event on which a turn is performed.