diff --git a/Assets/Scenes/MainGame.unity b/Assets/Scenes/MainGame.unity index 1a1139a..e100435 100644 --- a/Assets/Scenes/MainGame.unity +++ b/Assets/Scenes/MainGame.unity @@ -584,7 +584,7 @@ PrefabInstance: - target: {fileID: -8679921383154817045, guid: 1c178a87d052ee642a2205f8139d9c21, type: 3} propertyPath: m_RootOrder - value: 2 + value: 4 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: 1c178a87d052ee642a2205f8139d9c21, type: 3} @@ -1267,7 +1267,7 @@ PrefabInstance: - target: {fileID: -8679921383154817045, guid: 1c178a87d052ee642a2205f8139d9c21, type: 3} propertyPath: m_RootOrder - value: 3 + value: 5 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: 1c178a87d052ee642a2205f8139d9c21, type: 3} @@ -1518,7 +1518,7 @@ PrefabInstance: - target: {fileID: -8679921383154817045, guid: c277fb14a3cffbf498aaedfce222c56d, type: 3} propertyPath: m_RootOrder - value: 4 + value: 6 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: c277fb14a3cffbf498aaedfce222c56d, type: 3} @@ -1662,7 +1662,7 @@ PrefabInstance: - target: {fileID: -8679921383154817045, guid: bb03ebc0c542b4d4695036c9a9536009, type: 3} propertyPath: m_RootOrder - value: 5 + value: 7 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: bb03ebc0c542b4d4695036c9a9536009, type: 3} @@ -2546,7 +2546,7 @@ PrefabInstance: - target: {fileID: -8679921383154817045, guid: e855b3e0d6cb69d40b19e24bd2a1b3a0, type: 3} propertyPath: m_RootOrder - value: 1 + value: 3 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: e855b3e0d6cb69d40b19e24bd2a1b3a0, type: 3} @@ -4366,7 +4366,7 @@ PrefabInstance: - target: {fileID: -8679921383154817045, guid: 56fd8a0aa3b18fa43994e325b7d32a93, type: 3} propertyPath: m_RootOrder - value: 1 + value: 2 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: 56fd8a0aa3b18fa43994e325b7d32a93, type: 3} @@ -4376,7 +4376,7 @@ PrefabInstance: - target: {fileID: -8679921383154817045, guid: 56fd8a0aa3b18fa43994e325b7d32a93, type: 3} propertyPath: m_LocalPosition.y - value: 0.10000008 + value: 1.08 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: 56fd8a0aa3b18fa43994e325b7d32a93, type: 3} @@ -4506,12 +4506,12 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 963194225} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1, z: -10} + m_LocalRotation: {x: 0.17046925, y: 0.005578548, z: -0.0010177792, w: 0.98534673} + m_LocalPosition: {x: -0.102371216, y: 2.0939612, z: -1.6292458} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 0 + m_Father: {fileID: 1941989044} + m_RootOrder: 2 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &975865186 PrefabInstance: @@ -5193,7 +5193,7 @@ PrefabInstance: - target: {fileID: -8679921383154817045, guid: 38ce8aefc757b73458b452def63fc1a3, type: 3} propertyPath: m_RootOrder - value: 1 + value: 0 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: 38ce8aefc757b73458b452def63fc1a3, type: 3} @@ -5520,6 +5520,51 @@ Transform: type: 3} m_PrefabInstance: {fileID: 1186200390} m_PrefabAsset: {fileID: 0} +--- !u!1 &1209096398 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1209096400} + - component: {fileID: 1209096399} + m_Layer: 0 + m_Name: JoyconManager + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &1209096399 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1209096398} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 405bad29b2b998c408059182c189d9e9, type: 3} + m_Name: + m_EditorClassIdentifier: + EnableIMU: 1 + EnableLocalize: 1 +--- !u!4 &1209096400 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1209096398} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 11 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1001 &1216353526 PrefabInstance: m_ObjectHideFlags: 0 @@ -8895,6 +8940,65 @@ BoxCollider: serializedVersion: 2 m_Size: {x: 2.0196953, y: 0.2, z: 2.0206933} m_Center: {x: 0.0039343834, y: 0, z: 0.012269765} +--- !u!1 &1941989041 stripped +GameObject: + m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: 56fd8a0aa3b18fa43994e325b7d32a93, + type: 3} + m_PrefabInstance: {fileID: 962442943} + m_PrefabAsset: {fileID: 0} +--- !u!114 &1941989042 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1941989041} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: a3db9d02d190fcb46a9646c275a560e4, type: 3} + m_Name: + m_EditorClassIdentifier: + isDebug: 0 + body: {fileID: 1941989043} + constSpeed: 2 + constRotation: 150 + constUpForce: 5 +--- !u!54 &1941989043 +Rigidbody: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1941989041} + serializedVersion: 2 + m_Mass: 5 + m_Drag: 0 + m_AngularDrag: 0.05 + m_UseGravity: 1 + m_IsKinematic: 0 + m_Interpolate: 0 + m_Constraints: 112 + m_CollisionDetection: 0 +--- !u!4 &1941989044 stripped +Transform: + m_CorrespondingSourceObject: {fileID: -8679921383154817045, guid: 56fd8a0aa3b18fa43994e325b7d32a93, + type: 3} + m_PrefabInstance: {fileID: 962442943} + m_PrefabAsset: {fileID: 0} +--- !u!136 &1941989045 +CapsuleCollider: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1941989041} + m_Material: {fileID: 0} + m_IsTrigger: 0 + m_Enabled: 1 + m_Radius: 0.5 + m_Height: 1.4958494 + m_Direction: 1 + m_Center: {x: 0, y: 0.69886935, z: 0} --- !u!65 &1946133526 BoxCollider: m_ObjectHideFlags: 0 diff --git a/Assets/Scripts/JoyCon.meta b/Assets/Scripts/JoyCon.meta new file mode 100644 index 0000000..ec1542f --- /dev/null +++ b/Assets/Scripts/JoyCon.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 9a8928691c8f20649b560fbba94c0b66 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/JoyCon/HistoricalData.cs b/Assets/Scripts/JoyCon/HistoricalData.cs new file mode 100644 index 0000000..5bf1ca1 --- /dev/null +++ b/Assets/Scripts/JoyCon/HistoricalData.cs @@ -0,0 +1,26 @@ +namespace JoyCon +{ + public class HistoricalData + { + private readonly float[] _data; + private int _index; + + public float Total { get; private set; } + + public float Mean { get; private set; } + + public HistoricalData(int numberOfDataToKeep) + { + _data = new float[numberOfDataToKeep]; + } + + public void AddData(float newData) + { + Total -= _data[_index]; + Total += newData; + Mean = Total / _data.Length; + _data[_index] = newData; + _index = (_index + 1) % _data.Length; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/JoyCon/HistoricalData.cs.meta b/Assets/Scripts/JoyCon/HistoricalData.cs.meta new file mode 100644 index 0000000..059d273 --- /dev/null +++ b/Assets/Scripts/JoyCon/HistoricalData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 49cb22f532589c046ae8a23cda8f95a4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/JoyCon/JoyConBehaviour.cs b/Assets/Scripts/JoyCon/JoyConBehaviour.cs new file mode 100644 index 0000000..c49eeb9 --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyConBehaviour.cs @@ -0,0 +1,16 @@ +using UnityEngine; + +namespace JoyCon +{ + public class JoyConBehaviour : MonoBehaviour + { + protected JoyconManager JoyConManager; + protected const int JcLegInd = 0; + protected const int JcArmInd = 1; + + protected virtual void Start() + { + JoyConManager = JoyconManager.Instance; + } + } +} diff --git a/Assets/Scripts/JoyCon/JoyConBehaviour.cs.meta b/Assets/Scripts/JoyCon/JoyConBehaviour.cs.meta new file mode 100644 index 0000000..9d44ca5 --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyConBehaviour.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7eeb516887b07bd47bd1e8de5375d153 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/JoyCon/JoyConMovement.cs b/Assets/Scripts/JoyCon/JoyConMovement.cs new file mode 100644 index 0000000..1025587 --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyConMovement.cs @@ -0,0 +1,71 @@ +using UnityEngine; + +namespace JoyCon +{ + public class JoyConMovement : JoyConBehaviour + { + [Header("DEBUG movement")] + [SerializeField] public bool isDebug; + + [Header("References")] + [SerializeField] private Rigidbody body; + + [Header("Configuration")] + [SerializeField] private int constSpeed; + [SerializeField] private int constRotation; + [SerializeField] private int constUpForce; + + private HistoricalData _gyroMagnitude; + + private int _speed; + + private Vector3 _playerDirection; + + + protected override void Start() + { + if(!isDebug) + { + base.Start(); + _gyroMagnitude = new HistoricalData(5); + _speed = 0; + } + } + + private void Update() + { + if(!isDebug) + { + Joycon j = JoyConManager.GetJoycon(JcLegInd); + _playerDirection = j.GetButton(Joycon.Button.SHOULDER_2) ? Vector3.back : Vector3.forward; + } + } + + private void FixedUpdate() + { + if(!isDebug) + { + Joycon j = JoyConManager.GetJoycon(JcLegInd); + Joycon jArm = JoyConManager.GetJoycon(JcArmInd); + + _gyroMagnitude.AddData(j.GetGyro().magnitude); + + _speed = IsWalking() ? 4 : 0; + + body.transform.Rotate(0, jArm.GetStick()[0] * constRotation * Time.fixedDeltaTime, 0); + + body.AddRelativeForce(_playerDirection * (_speed * _gyroMagnitude.Total * constSpeed)); + body.AddForce(Vector3.up * constUpForce); + } + +#if DEBUG + float moveHorizontal = Input.GetAxis("Horizontal"); + float moveVertical = Input.GetAxis("Vertical"); + transform.Rotate(0.0f, moveHorizontal * constSpeed * 2, 0.0f); + transform.Translate(0, 0, moveVertical * constSpeed / 20); +#endif + } + + public bool IsWalking() => Mathf.Abs(_gyroMagnitude.Total) > 1; + } +} diff --git a/Assets/Scripts/JoyCon/JoyConMovement.cs.meta b/Assets/Scripts/JoyCon/JoyConMovement.cs.meta new file mode 100644 index 0000000..f8fb193 --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyConMovement.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a3db9d02d190fcb46a9646c275a560e4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/JoyCon/JoyConRotationFollow.cs b/Assets/Scripts/JoyCon/JoyConRotationFollow.cs new file mode 100644 index 0000000..0998200 --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyConRotationFollow.cs @@ -0,0 +1,20 @@ +using UnityEngine; + +namespace JoyCon +{ + public class JoyConRotationFollow : JoyConBehaviour + { + [SerializeField] private Transform ObjectParent; + [SerializeField] private Transform ObjectToRotate; + + private void Update() + { + Joycon j = JoyConManager.GetJoycon(JcArmInd); + if (j.GetButtonDown(Joycon.Button.SHOULDER_2)) + { + j.Recenter(); + } + ObjectToRotate.eulerAngles = ObjectParent.eulerAngles + j.GetVector().eulerAngles; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/JoyCon/JoyConRotationFollow.cs.meta b/Assets/Scripts/JoyCon/JoyConRotationFollow.cs.meta new file mode 100644 index 0000000..bf06dc3 --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyConRotationFollow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 3e725b7824fa7894080486a2470ec121 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/JoyCon/JoyConRumble.cs b/Assets/Scripts/JoyCon/JoyConRumble.cs new file mode 100644 index 0000000..f19ca91 --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyConRumble.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections; +using UnityEngine; + +namespace JoyCon +{ + public enum RumbleType + { + ConstantRumble, + LinearRumble, + PulseRumble, + } + + public class JoyConRumble : JoyConBehaviour + { + [SerializeField] private RumbleType RumbleType; + [SerializeField] private int RumbleLocation; + [SerializeField] private int _rumbleDuration = 250; + [SerializeField] private int Rep = 10; + private IEnumerator _coroutine; + private const float MaxRumbleFrequency = 100f; + + public void Set(RumbleType rumbleType, int rumbleLocation, int rumbleDuration, int rep =10) + { + RumbleType = rumbleType; + RumbleLocation = rumbleLocation; + _rumbleDuration = rumbleDuration; + Rep = rep; + } + + protected override void Start() + { + base.Start(); + switch (RumbleType) + { + case RumbleType.ConstantRumble: + _coroutine = ConstantRumble(); + break; + case RumbleType.LinearRumble: + _coroutine = LinearRumble(); + break; + case RumbleType.PulseRumble: + _coroutine = PulseRumble(); + break; + default: + throw new ArgumentOutOfRangeException(); + } + StartCoroutine(_coroutine); + } + + private IEnumerator ConstantRumble() + { + yield return new WaitForSeconds(0.1f); + Joycon j = JoyConManager.GetJoycon(RumbleLocation); + j.SetRumble(150, 150, 0.6f, _rumbleDuration); + yield return new WaitForSeconds(0.1f); + Destroy(this); + } + + private IEnumerator LinearRumble() + { + const float i = 1.05f; + int time = 0; + float maxFrequency = 30f; + yield return new WaitForSeconds(0.1f); + Joycon j = JoyConManager.GetJoycon(RumbleLocation); + while(time < Rep) + { + j.SetRumble(25, maxFrequency, 0.6f, _rumbleDuration); + time++; + maxFrequency = Mathf.Min(maxFrequency * i, MaxRumbleFrequency); + yield return new WaitForSeconds(0.1f); + } + Destroy(this); + } + + private IEnumerator PulseRumble() { + yield return new WaitForSeconds(0.1f); + int time = 0; + Joycon j = JoyConManager.GetJoycon(RumbleLocation); + while (time < Rep) + { + j.SetRumble(50, 150, 0.6f, 500); + time++; + yield return new WaitForSeconds(1f); + } + Destroy(this); + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/JoyCon/JoyConRumble.cs.meta b/Assets/Scripts/JoyCon/JoyConRumble.cs.meta new file mode 100644 index 0000000..9ac5eb5 --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyConRumble.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: df5d6598237e80b4b8ea7afa4d84e1d4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/JoyCon/JoyconDemo.cs b/Assets/Scripts/JoyCon/JoyconDemo.cs new file mode 100644 index 0000000..435948a --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyconDemo.cs @@ -0,0 +1,84 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class JoyconDemo : MonoBehaviour { + + private List joycons; + + // Values made available via Unity + public float[] stick; + public Vector3 gyro; + public Vector3 accel; + public int jc_ind = 0; + public Quaternion orientation; + + void Start () + { + gyro = new Vector3(0, 0, 0); + accel = new Vector3(0, 0, 0); + // get the public Joycon array attached to the JoyconManager in scene + joycons = JoyconManager.Instance.ConnectedJoycons; + if (joycons.Count < jc_ind+1){ + Destroy(gameObject); + } + } + + // Update is called once per frame + void Update () { + // make sure the Joycon only gets checked if attached + if (joycons.Count > 0) + { + Joycon j = joycons [jc_ind]; + // GetButtonDown checks if a button has been pressed (not held) + if (j.GetButtonDown(Joycon.Button.SHOULDER_2)) + { + Debug.Log ("Shoulder button 2 pressed"); + // GetStick returns a 2-element vector with x/y joystick components + Debug.Log(string.Format("Stick x: {0:N} Stick y: {1:N}",j.GetStick()[0],j.GetStick()[1])); + + // Joycon has no magnetometer, so it cannot accurately determine its yaw value. Joycon.Recenter allows the user to reset the yaw value. + j.Recenter (); + } + // GetButtonDown checks if a button has been released + if (j.GetButtonUp (Joycon.Button.SHOULDER_2)) + { + Debug.Log ("Shoulder button 2 released"); + } + // GetButtonDown checks if a button is currently down (pressed or held) + if (j.GetButton (Joycon.Button.SHOULDER_2)) + { + Debug.Log ("Shoulder button 2 held"); + } + + if (j.GetButtonDown (Joycon.Button.DPAD_DOWN)) { + Debug.Log ("Rumble"); + + // Rumble for 200 milliseconds, with low frequency rumble at 160 Hz and high frequency rumble at 320 Hz. For more information check: + // https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering/blob/master/rumble_data_table.md + + j.SetRumble (160, 320, 0.6f, 200); + + // The last argument (time) in SetRumble is optional. Call it with three arguments to turn it on without telling it when to turn off. + // (Useful for dynamically changing rumble values.) + // Then call SetRumble(0,0,0) when you want to turn it off. + } + + stick = j.GetStick(); + + // Gyro values: x, y, z axis values (in radians per second) + gyro = j.GetGyro(); + + // Accel values: x, y, z axis values (in Gs) + accel = j.GetAccel(); + + orientation = j.GetVector(); + /*if (j.GetButton(Joycon.Button.DPAD_UP)){ + gameObject.GetComponent().material.color = Color.red; + } else{ + gameObject.GetComponent().material.color = Color.blue; + }*/ + gameObject.transform.rotation = orientation; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/JoyCon/JoyconDemo.cs.meta b/Assets/Scripts/JoyCon/JoyconDemo.cs.meta new file mode 100644 index 0000000..513919f --- /dev/null +++ b/Assets/Scripts/JoyCon/JoyconDemo.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0a51932488397274d9623a84c8a1fff2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: