From df0819acab5658a1874cec30400a41b432a13c7a Mon Sep 17 00:00:00 2001 From: Konstantin Kohl Date: Wed, 23 Oct 2019 16:34:15 +0200 Subject: [PATCH 1/2] Implemented gamepad support --- .../Generated/SimulatorControls.cs | 228 ++++++++++++++++++ .../Controllers/SimulatorCameraController.cs | 86 ++++++- .../SimulatorControls.inputactions | 188 +++++++++++++++ Assets/Scripts/Sensors/ManualControlSensor.cs | 46 +++- 4 files changed, 546 insertions(+), 2 deletions(-) diff --git a/Assets/Scripts/Controllers/Generated/SimulatorControls.cs b/Assets/Scripts/Controllers/Generated/SimulatorControls.cs index 929650590..3b83753b5 100644 --- a/Assets/Scripts/Controllers/Generated/SimulatorControls.cs +++ b/Assets/Scripts/Controllers/Generated/SimulatorControls.cs @@ -19,6 +19,28 @@ public SimulatorControls() ""name"": ""Vehicle"", ""id"": ""eb4888fe-9130-4adc-a90b-bf846f8245e1"", ""actions"": [ + { + ""name"": ""ControllerSteering"", + ""id"": ""0f0f4029-be46-40fc-b132-611bd8141aec"", + ""expectedControlLayout"": ""Stick"", + ""continuous"": true, + ""passThrough"": true, + ""initialStateCheck"": false, + ""processors"": """", + ""interactions"": """", + ""bindings"": [] + }, + { + ""name"": ""ControllerAcceleration"", + ""id"": ""a1580238-3168-406e-a230-8c1594b9087f"", + ""expectedControlLayout"": ""Axis"", + ""continuous"": false, + ""passThrough"": false, + ""initialStateCheck"": false, + ""processors"": """", + ""interactions"": """", + ""bindings"": [] + }, { ""name"": ""Direction"", ""id"": ""74a3751d-7b46-43ea-8db5-6ff3c1a8a03d"", @@ -214,6 +236,18 @@ public SimulatorControls() ""isPartOfComposite"": false, ""modifiers"": """" }, + { + ""name"": """", + ""id"": ""875fed1c-0f90-4ee5-a7e7-1bbb47e113fb"", + ""path"": ""/buttonNorth"", + ""interactions"": ""Press"", + ""processors"": """", + ""groups"": """", + ""action"": ""HeadLights"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, { ""name"": """", ""id"": ""f071a7ab-903c-4f73-9277-245135e92351"", @@ -226,6 +260,18 @@ public SimulatorControls() ""isPartOfComposite"": false, ""modifiers"": """" }, + { + ""name"": """", + ""id"": ""5430d1d2-6cf4-4cb4-a986-4d572f8082f9"", + ""path"": ""/leftShoulder"", + ""interactions"": ""Press"", + ""processors"": """", + ""groups"": """", + ""action"": ""IndicatorLeft"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, { ""name"": """", ""id"": ""1c9be836-d82f-4f2f-b7fb-17cfc3ba4c42"", @@ -238,6 +284,18 @@ public SimulatorControls() ""isPartOfComposite"": false, ""modifiers"": """" }, + { + ""name"": """", + ""id"": ""b036930b-6827-456a-989c-f794dac39fcf"", + ""path"": ""/rightShoulder"", + ""interactions"": ""Press"", + ""processors"": """", + ""groups"": """", + ""action"": ""IndicatorRight"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, { ""name"": """", ""id"": ""102d8903-7ece-4382-8884-6ef9a3895b16"", @@ -274,6 +332,18 @@ public SimulatorControls() ""isPartOfComposite"": false, ""modifiers"": """" }, + { + ""name"": """", + ""id"": ""b3af9d0f-1716-41ac-909f-cc2d6390bc49"", + ""path"": ""/dpad/up"", + ""interactions"": ""Press"", + ""processors"": """", + ""groups"": """", + ""action"": ""ShiftFirst"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, { ""name"": """", ""id"": ""9f3653bb-d12f-464e-91a1-1e38d5432b7c"", @@ -286,6 +356,18 @@ public SimulatorControls() ""isPartOfComposite"": false, ""modifiers"": """" }, + { + ""name"": """", + ""id"": ""8675dfb7-a697-49e7-b6dc-cb9f821a784b"", + ""path"": ""/dpad/down"", + ""interactions"": ""Press"", + ""processors"": """", + ""groups"": """", + ""action"": ""ShiftReverse"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, { ""name"": """", ""id"": ""0b7bdefd-cff1-47e4-82a9-0a835e3d4539"", @@ -298,6 +380,18 @@ public SimulatorControls() ""isPartOfComposite"": false, ""modifiers"": """" }, + { + ""name"": """", + ""id"": ""5bbf6e96-bc8d-4ef0-9abe-654d1e1786e3"", + ""path"": ""/buttonSouth"", + ""interactions"": ""Press"", + ""processors"": """", + ""groups"": """", + ""action"": ""ParkingBrake"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, { ""name"": """", ""id"": ""9b612607-638c-43ac-8787-e9a2df3ed7e6"", @@ -321,6 +415,54 @@ public SimulatorControls() ""isComposite"": false, ""isPartOfComposite"": false, ""modifiers"": """" + }, + { + ""name"": """", + ""id"": ""7574ccb9-ca8e-43a4-9792-8cae41bff976"", + ""path"": ""/leftStick"", + ""interactions"": """", + ""processors"": ""StickDeadzone(min=0.1)"", + ""groups"": """", + ""action"": ""ControllerSteering"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, + { + ""name"": ""Throttle_Brake"", + ""id"": ""7399bd92-679e-4f6a-9652-c7856003acf2"", + ""path"": ""1DAxis"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""ControllerAcceleration"", + ""isComposite"": true, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, + { + ""name"": ""negative"", + ""id"": ""769465d1-2550-4306-b97b-770804ff5a9c"", + ""path"": ""/leftTrigger"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""ControllerAcceleration"", + ""isComposite"": false, + ""isPartOfComposite"": true, + ""modifiers"": """" + }, + { + ""name"": ""positive"", + ""id"": ""56d9c3ba-b989-476b-b330-efde12b68b03"", + ""path"": ""/rightTrigger"", + ""interactions"": """", + ""processors"": """", + ""groups"": """", + ""action"": ""ControllerAcceleration"", + ""isComposite"": false, + ""isPartOfComposite"": true, + ""modifiers"": """" } ] }, @@ -361,6 +503,17 @@ public SimulatorControls() ""interactions"": """", ""bindings"": [] }, + { + ""name"": ""GamepadView"", + ""id"": ""fbb8d49b-79f7-4673-908c-4941316a4138"", + ""expectedControlLayout"": ""Stick"", + ""continuous"": true, + ""passThrough"": false, + ""initialStateCheck"": true, + ""processors"": """", + ""interactions"": """", + ""bindings"": [] + }, { ""name"": ""MouseLeft"", ""id"": ""18683cc6-4aa4-44ca-9229-acb759267a4b"", @@ -448,6 +601,17 @@ public SimulatorControls() ""processors"": """", ""interactions"": """", ""bindings"": [] + }, + { + ""name"": ""FlipCam"", + ""id"": ""b5b32f81-097a-40fc-91d4-7e965a377e3c"", + ""expectedControlLayout"": """", + ""continuous"": true, + ""passThrough"": true, + ""initialStateCheck"": false, + ""processors"": """", + ""interactions"": """", + ""bindings"": [] } ], ""bindings"": [ @@ -678,6 +842,30 @@ public SimulatorControls() ""isComposite"": false, ""isPartOfComposite"": true, ""modifiers"": """" + }, + { + ""name"": """", + ""id"": ""7368c8b8-ecd6-4864-8a85-a0859a9da937"", + ""path"": ""/rightStick"", + ""interactions"": """", + ""processors"": ""StickDeadzone(min=0.1)"", + ""groups"": """", + ""action"": ""GamepadView"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" + }, + { + ""name"": """", + ""id"": ""5e4b8691-a2f1-4fe3-b10e-046d9f8d376d"", + ""path"": ""/rightStickPress"", + ""interactions"": ""Hold(duration=0.01)"", + ""processors"": """", + ""groups"": """", + ""action"": ""FlipCam"", + ""isComposite"": false, + ""isPartOfComposite"": false, + ""modifiers"": """" } ] }, @@ -917,6 +1105,8 @@ public SimulatorControls() }"); // Vehicle m_Vehicle = asset.GetActionMap("Vehicle"); + m_Vehicle_ControllerSteering = m_Vehicle.GetAction("ControllerSteering"); + m_Vehicle_ControllerAcceleration = m_Vehicle.GetAction("ControllerAcceleration"); m_Vehicle_Direction = m_Vehicle.GetAction("Direction"); m_Vehicle_HeadLights = m_Vehicle.GetAction("HeadLights"); m_Vehicle_IndicatorLeft = m_Vehicle.GetAction("IndicatorLeft"); @@ -933,6 +1123,7 @@ public SimulatorControls() m_Camera_Direction = m_Camera.GetAction("Direction"); m_Camera_Elevation = m_Camera.GetAction("Elevation"); m_Camera_MouseDelta = m_Camera.GetAction("MouseDelta"); + m_Camera_GamepadView = m_Camera.GetAction("GamepadView"); m_Camera_MouseLeft = m_Camera.GetAction("MouseLeft"); m_Camera_MouseRight = m_Camera.GetAction("MouseRight"); m_Camera_MouseMiddle = m_Camera.GetAction("MouseMiddle"); @@ -941,6 +1132,7 @@ public SimulatorControls() m_Camera_Boost = m_Camera.GetAction("Boost"); m_Camera_ToggleState = m_Camera.GetAction("ToggleState"); m_Camera_Zoom = m_Camera.GetAction("Zoom"); + m_Camera_FlipCam = m_Camera.GetAction("FlipCam"); // Simulator m_Simulator = asset.GetActionMap("Simulator"); m_Simulator_ToggleNPCS = m_Simulator.GetAction("ToggleNPCS"); @@ -1000,6 +1192,8 @@ public void Disable() // Vehicle private InputActionMap m_Vehicle; private IVehicleActions m_VehicleActionsCallbackInterface; + private InputAction m_Vehicle_ControllerSteering; + private InputAction m_Vehicle_ControllerAcceleration; private InputAction m_Vehicle_Direction; private InputAction m_Vehicle_HeadLights; private InputAction m_Vehicle_IndicatorLeft; @@ -1015,6 +1209,8 @@ public struct VehicleActions { private SimulatorControls m_Wrapper; public VehicleActions(SimulatorControls wrapper) { m_Wrapper = wrapper; } + public InputAction @ControllerSteering { get { return m_Wrapper.m_Vehicle_ControllerSteering; } } + public InputAction @ControllerAcceleration { get { return m_Wrapper.m_Vehicle_ControllerAcceleration; } } public InputAction @Direction { get { return m_Wrapper.m_Vehicle_Direction; } } public InputAction @HeadLights { get { return m_Wrapper.m_Vehicle_HeadLights; } } public InputAction @IndicatorLeft { get { return m_Wrapper.m_Vehicle_IndicatorLeft; } } @@ -1036,6 +1232,12 @@ public void SetCallbacks(IVehicleActions instance) { if (m_Wrapper.m_VehicleActionsCallbackInterface != null) { + ControllerSteering.started -= m_Wrapper.m_VehicleActionsCallbackInterface.OnControllerSteering; + ControllerSteering.performed -= m_Wrapper.m_VehicleActionsCallbackInterface.OnControllerSteering; + ControllerSteering.canceled -= m_Wrapper.m_VehicleActionsCallbackInterface.OnControllerSteering; + ControllerAcceleration.started -= m_Wrapper.m_VehicleActionsCallbackInterface.OnControllerAcceleration; + ControllerAcceleration.performed -= m_Wrapper.m_VehicleActionsCallbackInterface.OnControllerAcceleration; + ControllerAcceleration.canceled -= m_Wrapper.m_VehicleActionsCallbackInterface.OnControllerAcceleration; Direction.started -= m_Wrapper.m_VehicleActionsCallbackInterface.OnDirection; Direction.performed -= m_Wrapper.m_VehicleActionsCallbackInterface.OnDirection; Direction.canceled -= m_Wrapper.m_VehicleActionsCallbackInterface.OnDirection; @@ -1073,6 +1275,12 @@ public void SetCallbacks(IVehicleActions instance) m_Wrapper.m_VehicleActionsCallbackInterface = instance; if (instance != null) { + ControllerSteering.started += instance.OnControllerSteering; + ControllerSteering.performed += instance.OnControllerSteering; + ControllerSteering.canceled += instance.OnControllerSteering; + ControllerAcceleration.started += instance.OnControllerAcceleration; + ControllerAcceleration.performed += instance.OnControllerAcceleration; + ControllerAcceleration.canceled += instance.OnControllerAcceleration; Direction.started += instance.OnDirection; Direction.performed += instance.OnDirection; Direction.canceled += instance.OnDirection; @@ -1123,6 +1331,7 @@ public VehicleActions @Vehicle private InputAction m_Camera_Direction; private InputAction m_Camera_Elevation; private InputAction m_Camera_MouseDelta; + private InputAction m_Camera_GamepadView; private InputAction m_Camera_MouseLeft; private InputAction m_Camera_MouseRight; private InputAction m_Camera_MouseMiddle; @@ -1131,6 +1340,7 @@ public VehicleActions @Vehicle private InputAction m_Camera_Boost; private InputAction m_Camera_ToggleState; private InputAction m_Camera_Zoom; + private InputAction m_Camera_FlipCam; public struct CameraActions { private SimulatorControls m_Wrapper; @@ -1138,6 +1348,7 @@ public struct CameraActions public InputAction @Direction { get { return m_Wrapper.m_Camera_Direction; } } public InputAction @Elevation { get { return m_Wrapper.m_Camera_Elevation; } } public InputAction @MouseDelta { get { return m_Wrapper.m_Camera_MouseDelta; } } + public InputAction @GamepadView { get { return m_Wrapper.m_Camera_GamepadView; } } public InputAction @MouseLeft { get { return m_Wrapper.m_Camera_MouseLeft; } } public InputAction @MouseRight { get { return m_Wrapper.m_Camera_MouseRight; } } public InputAction @MouseMiddle { get { return m_Wrapper.m_Camera_MouseMiddle; } } @@ -1146,6 +1357,7 @@ public struct CameraActions public InputAction @Boost { get { return m_Wrapper.m_Camera_Boost; } } public InputAction @ToggleState { get { return m_Wrapper.m_Camera_ToggleState; } } public InputAction @Zoom { get { return m_Wrapper.m_Camera_Zoom; } } + public InputAction @FlipCam { get { return m_Wrapper.m_Camera_FlipCam; } } public InputActionMap Get() { return m_Wrapper.m_Camera; } public void Enable() { Get().Enable(); } public void Disable() { Get().Disable(); } @@ -1165,6 +1377,9 @@ public void SetCallbacks(ICameraActions instance) MouseDelta.started -= m_Wrapper.m_CameraActionsCallbackInterface.OnMouseDelta; MouseDelta.performed -= m_Wrapper.m_CameraActionsCallbackInterface.OnMouseDelta; MouseDelta.canceled -= m_Wrapper.m_CameraActionsCallbackInterface.OnMouseDelta; + GamepadView.started -= m_Wrapper.m_CameraActionsCallbackInterface.OnGamepadView; + GamepadView.performed -= m_Wrapper.m_CameraActionsCallbackInterface.OnGamepadView; + GamepadView.canceled -= m_Wrapper.m_CameraActionsCallbackInterface.OnGamepadView; MouseLeft.started -= m_Wrapper.m_CameraActionsCallbackInterface.OnMouseLeft; MouseLeft.performed -= m_Wrapper.m_CameraActionsCallbackInterface.OnMouseLeft; MouseLeft.canceled -= m_Wrapper.m_CameraActionsCallbackInterface.OnMouseLeft; @@ -1189,6 +1404,9 @@ public void SetCallbacks(ICameraActions instance) Zoom.started -= m_Wrapper.m_CameraActionsCallbackInterface.OnZoom; Zoom.performed -= m_Wrapper.m_CameraActionsCallbackInterface.OnZoom; Zoom.canceled -= m_Wrapper.m_CameraActionsCallbackInterface.OnZoom; + FlipCam.started -= m_Wrapper.m_CameraActionsCallbackInterface.OnFlipCam; + FlipCam.performed -= m_Wrapper.m_CameraActionsCallbackInterface.OnFlipCam; + FlipCam.canceled -= m_Wrapper.m_CameraActionsCallbackInterface.OnFlipCam; } m_Wrapper.m_CameraActionsCallbackInterface = instance; if (instance != null) @@ -1202,6 +1420,9 @@ public void SetCallbacks(ICameraActions instance) MouseDelta.started += instance.OnMouseDelta; MouseDelta.performed += instance.OnMouseDelta; MouseDelta.canceled += instance.OnMouseDelta; + GamepadView.started += instance.OnGamepadView; + GamepadView.performed += instance.OnGamepadView; + GamepadView.canceled += instance.OnGamepadView; MouseLeft.started += instance.OnMouseLeft; MouseLeft.performed += instance.OnMouseLeft; MouseLeft.canceled += instance.OnMouseLeft; @@ -1226,6 +1447,9 @@ public void SetCallbacks(ICameraActions instance) Zoom.started += instance.OnZoom; Zoom.performed += instance.OnZoom; Zoom.canceled += instance.OnZoom; + FlipCam.started += instance.OnFlipCam; + FlipCam.performed += instance.OnFlipCam; + FlipCam.canceled += instance.OnFlipCam; } } } @@ -1310,6 +1534,8 @@ public SimulatorActions @Simulator } public interface IVehicleActions { + void OnControllerSteering(InputAction.CallbackContext context); + void OnControllerAcceleration(InputAction.CallbackContext context); void OnDirection(InputAction.CallbackContext context); void OnHeadLights(InputAction.CallbackContext context); void OnIndicatorLeft(InputAction.CallbackContext context); @@ -1327,6 +1553,7 @@ public interface ICameraActions void OnDirection(InputAction.CallbackContext context); void OnElevation(InputAction.CallbackContext context); void OnMouseDelta(InputAction.CallbackContext context); + void OnGamepadView(InputAction.CallbackContext context); void OnMouseLeft(InputAction.CallbackContext context); void OnMouseRight(InputAction.CallbackContext context); void OnMouseMiddle(InputAction.CallbackContext context); @@ -1335,6 +1562,7 @@ public interface ICameraActions void OnBoost(InputAction.CallbackContext context); void OnToggleState(InputAction.CallbackContext context); void OnZoom(InputAction.CallbackContext context); + void OnFlipCam(InputAction.CallbackContext context); } public interface ISimulatorActions { diff --git a/Assets/Scripts/Controllers/SimulatorCameraController.cs b/Assets/Scripts/Controllers/SimulatorCameraController.cs index b481dd72f..7359f97ed 100644 --- a/Assets/Scripts/Controllers/SimulatorCameraController.cs +++ b/Assets/Scripts/Controllers/SimulatorCameraController.cs @@ -43,6 +43,16 @@ public class SimulatorCameraController : MonoBehaviour public CameraStateType CurrentCameraState = CameraStateType.Free; + public bool lockFreeCamRotationToVehicle = true; + private Vector2 gamepadViewInput; + private float camResetSpeed = 25f; + private float camLookTurnSpeed = 200.0f; + private float camTiltTurnSpeed = 200.0f; + private float camResetThreshold = 10.0f; + private Vector2 targetCamRotation = Vector2.zero; + public Rigidbody targetRB; + private bool camIsFlipped = false; + private void Awake() { thisCamera = GetComponentInChildren(); @@ -81,6 +91,13 @@ private void Awake() controls.Camera.MouseRight.performed += ctx => mouseRight = ctx.ReadValue(); controls.Camera.MouseRight.canceled += ctx => mouseRight = ctx.ReadValue(); + controls.Camera.GamepadView.started += ctx => gamepadViewInput = GetScaledLookInput(ctx.ReadValue()); + controls.Camera.GamepadView.performed += ctx => gamepadViewInput = GetScaledLookInput(ctx.ReadValue()); + controls.Camera.GamepadView.canceled += ctx => gamepadViewInput = Vector2.zero; + + controls.Camera.FlipCam.performed += ctx => camIsFlipped = true; + controls.Camera.FlipCam.canceled += ctx => camIsFlipped = false; + //controls.Camera.MouseMiddle.performed += ctx => ResetFollowRotation(); // TODO broken in package currently https://github.com/Unity-Technologies/InputSystem/issues/647 @@ -89,6 +106,11 @@ private void Awake() //controls.Camera.MouseScroll.canceled += ctx => mouseScroll = Vector2.zero; } + private Vector2 GetScaledLookInput(Vector2 rawLookInput) + { + return new Vector2(rawLookInput.x * camTiltTurnSpeed * Time.deltaTime, rawLookInput.y * camLookTurnSpeed * Time.deltaTime); + } + private void Start() { targetTiltFree = transform.eulerAngles.x; @@ -148,7 +170,14 @@ private void LateUpdate() UpdateFreeCamera(); break; case CameraStateType.Follow: - UpdateFollowCamera(); + if(lockFreeCamRotationToVehicle) + { + UpdateFollowLinkedRotationCamera(); + } + else + { + UpdateFollowCamera(); + } break; } } @@ -205,6 +234,57 @@ private void UpdateFollowCamera() transform.position = Vector3.SmoothDamp(transform.position, targetObject.position, ref targetVelocity, 0.1f); } + private void UpdateFollowLinkedRotationCamera() + { + Debug.Assert(targetObject != null); + + var dist = Vector3.Distance(thisCamera.transform.position, targetObject.position); + if (dist < 3) + thisCamera.transform.localPosition = Vector3.MoveTowards(thisCamera.transform.localPosition, thisCamera.transform.InverseTransformPoint(targetObject.position), -Time.unscaledDeltaTime); + else if (dist > 30) + thisCamera.transform.localPosition = Vector3.MoveTowards(thisCamera.transform.localPosition, thisCamera.transform.InverseTransformPoint(targetObject.position), Time.unscaledDeltaTime); + else if (zoomInput != 0) + thisCamera.transform.localPosition = Vector3.MoveTowards(thisCamera.transform.localPosition, thisCamera.transform.InverseTransformPoint(targetObject.position), Time.unscaledDeltaTime * zoomInput * 10f * (boost == 1 ? 10f : 1f)); + + Vector2 viewInput = gamepadViewInput; + if (mouseRight == 1) + { + viewInput = mouseInput; + } + + if(targetCamRotation.y > 180.0f) + { + targetCamRotation.y -= 360.0f; + } + else if(targetCamRotation.y < -180.0f) + { + targetCamRotation.y += 360.0f; + } + + if(viewInput.sqrMagnitude <= 0.1 && targetRB != null && targetRB.velocity.sqrMagnitude > camResetThreshold) + { + targetCamRotation = Vector2.MoveTowards(targetCamRotation, Vector2.zero, Time.deltaTime * camResetSpeed); + } + else + { + targetCamRotation.y += viewInput.x * 0.25f; + targetCamRotation.x += viewInput.y * 0.1f * (inverted ? -1 : 1); + targetCamRotation.x = Mathf.Clamp(targetCamRotation.x, -15, 65); + } + + Quaternion targetQuat = Quaternion.LookRotation(camIsFlipped ? -targetObject.forward : targetObject.forward); + float tiltFree = targetCamRotation.x + targetQuat.eulerAngles.x; + float lookFree = targetCamRotation.y + targetQuat.eulerAngles.y; + + if (tiltFree > 180) + { + tiltFree -= 360; + } + + transform.rotation = Quaternion.Euler(tiltFree, lookFree, 0.0f); + transform.position = Vector3.SmoothDamp(transform.position, targetObject.position, ref targetVelocity, 0.1f); + } + public void SetFollowCameraState(GameObject target) { Debug.Assert(target != null); @@ -219,6 +299,8 @@ public void SetFollowCameraState(GameObject target) targetTiltFree = transform.eulerAngles.x; targetLookFree = transform.eulerAngles.y; SimulatorManager.Instance.UIManager?.SetCameraButtonState(); + targetRB = target.GetComponent(); + targetCamRotation = transform.eulerAngles; } public void SetFreeCameraState() @@ -232,5 +314,7 @@ public void SetFreeCameraState() targetTiltFree = transform.eulerAngles.x; targetLookFree = transform.eulerAngles.y; SimulatorManager.Instance.UIManager?.SetCameraButtonState(); + targetRB = null; + targetCamRotation = transform.eulerAngles; } } diff --git a/Assets/Scripts/Controllers/SimulatorControls.inputactions b/Assets/Scripts/Controllers/SimulatorControls.inputactions index 665da0ff1..a195c50f2 100644 --- a/Assets/Scripts/Controllers/SimulatorControls.inputactions +++ b/Assets/Scripts/Controllers/SimulatorControls.inputactions @@ -5,6 +5,28 @@ "name": "Vehicle", "id": "eb4888fe-9130-4adc-a90b-bf846f8245e1", "actions": [ + { + "name": "ControllerSteering", + "id": "0f0f4029-be46-40fc-b132-611bd8141aec", + "expectedControlLayout": "Stick", + "continuous": true, + "passThrough": true, + "initialStateCheck": false, + "processors": "", + "interactions": "", + "bindings": [] + }, + { + "name": "ControllerAcceleration", + "id": "a1580238-3168-406e-a230-8c1594b9087f", + "expectedControlLayout": "Axis", + "continuous": false, + "passThrough": false, + "initialStateCheck": false, + "processors": "", + "interactions": "", + "bindings": [] + }, { "name": "Direction", "id": "74a3751d-7b46-43ea-8db5-6ff3c1a8a03d", @@ -200,6 +222,18 @@ "isPartOfComposite": false, "modifiers": "" }, + { + "name": "", + "id": "875fed1c-0f90-4ee5-a7e7-1bbb47e113fb", + "path": "/buttonNorth", + "interactions": "Press", + "processors": "", + "groups": "", + "action": "HeadLights", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" + }, { "name": "", "id": "f071a7ab-903c-4f73-9277-245135e92351", @@ -212,6 +246,18 @@ "isPartOfComposite": false, "modifiers": "" }, + { + "name": "", + "id": "5430d1d2-6cf4-4cb4-a986-4d572f8082f9", + "path": "/leftShoulder", + "interactions": "Press", + "processors": "", + "groups": "", + "action": "IndicatorLeft", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" + }, { "name": "", "id": "1c9be836-d82f-4f2f-b7fb-17cfc3ba4c42", @@ -224,6 +270,18 @@ "isPartOfComposite": false, "modifiers": "" }, + { + "name": "", + "id": "b036930b-6827-456a-989c-f794dac39fcf", + "path": "/rightShoulder", + "interactions": "Press", + "processors": "", + "groups": "", + "action": "IndicatorRight", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" + }, { "name": "", "id": "102d8903-7ece-4382-8884-6ef9a3895b16", @@ -260,6 +318,18 @@ "isPartOfComposite": false, "modifiers": "" }, + { + "name": "", + "id": "b3af9d0f-1716-41ac-909f-cc2d6390bc49", + "path": "/dpad/up", + "interactions": "Press", + "processors": "", + "groups": "", + "action": "ShiftFirst", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" + }, { "name": "", "id": "9f3653bb-d12f-464e-91a1-1e38d5432b7c", @@ -272,6 +342,18 @@ "isPartOfComposite": false, "modifiers": "" }, + { + "name": "", + "id": "8675dfb7-a697-49e7-b6dc-cb9f821a784b", + "path": "/dpad/down", + "interactions": "Press", + "processors": "", + "groups": "", + "action": "ShiftReverse", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" + }, { "name": "", "id": "0b7bdefd-cff1-47e4-82a9-0a835e3d4539", @@ -284,6 +366,18 @@ "isPartOfComposite": false, "modifiers": "" }, + { + "name": "", + "id": "5bbf6e96-bc8d-4ef0-9abe-654d1e1786e3", + "path": "/buttonSouth", + "interactions": "Press", + "processors": "", + "groups": "", + "action": "ParkingBrake", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" + }, { "name": "", "id": "9b612607-638c-43ac-8787-e9a2df3ed7e6", @@ -307,6 +401,54 @@ "isComposite": false, "isPartOfComposite": false, "modifiers": "" + }, + { + "name": "", + "id": "7574ccb9-ca8e-43a4-9792-8cae41bff976", + "path": "/leftStick", + "interactions": "", + "processors": "StickDeadzone(min=0.1)", + "groups": "", + "action": "ControllerSteering", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" + }, + { + "name": "Throttle_Brake", + "id": "7399bd92-679e-4f6a-9652-c7856003acf2", + "path": "1DAxis", + "interactions": "", + "processors": "", + "groups": "", + "action": "ControllerAcceleration", + "isComposite": true, + "isPartOfComposite": false, + "modifiers": "" + }, + { + "name": "negative", + "id": "769465d1-2550-4306-b97b-770804ff5a9c", + "path": "/leftTrigger", + "interactions": "", + "processors": "", + "groups": "", + "action": "ControllerAcceleration", + "isComposite": false, + "isPartOfComposite": true, + "modifiers": "" + }, + { + "name": "positive", + "id": "56d9c3ba-b989-476b-b330-efde12b68b03", + "path": "/rightTrigger", + "interactions": "", + "processors": "", + "groups": "", + "action": "ControllerAcceleration", + "isComposite": false, + "isPartOfComposite": true, + "modifiers": "" } ] }, @@ -347,6 +489,17 @@ "interactions": "", "bindings": [] }, + { + "name": "GamepadView", + "id": "fbb8d49b-79f7-4673-908c-4941316a4138", + "expectedControlLayout": "Stick", + "continuous": true, + "passThrough": false, + "initialStateCheck": true, + "processors": "", + "interactions": "", + "bindings": [] + }, { "name": "MouseLeft", "id": "18683cc6-4aa4-44ca-9229-acb759267a4b", @@ -434,6 +587,17 @@ "processors": "", "interactions": "", "bindings": [] + }, + { + "name": "FlipCam", + "id": "b5b32f81-097a-40fc-91d4-7e965a377e3c", + "expectedControlLayout": "", + "continuous": true, + "passThrough": true, + "initialStateCheck": false, + "processors": "", + "interactions": "", + "bindings": [] } ], "bindings": [ @@ -664,6 +828,30 @@ "isComposite": false, "isPartOfComposite": true, "modifiers": "" + }, + { + "name": "", + "id": "7368c8b8-ecd6-4864-8a85-a0859a9da937", + "path": "/rightStick", + "interactions": "", + "processors": "StickDeadzone(min=0.1)", + "groups": "", + "action": "GamepadView", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" + }, + { + "name": "", + "id": "5e4b8691-a2f1-4fe3-b10e-046d9f8d376d", + "path": "/rightStickPress", + "interactions": "Hold(duration=0.01)", + "processors": "", + "groups": "", + "action": "FlipCam", + "isComposite": false, + "isPartOfComposite": false, + "modifiers": "" } ] }, diff --git a/Assets/Scripts/Sensors/ManualControlSensor.cs b/Assets/Scripts/Sensors/ManualControlSensor.cs index 0e02621fd..b171f1621 100644 --- a/Assets/Scripts/Sensors/ManualControlSensor.cs +++ b/Assets/Scripts/Sensors/ManualControlSensor.cs @@ -26,6 +26,8 @@ public class ManualControlSensor : SensorBase, IVehicleInputs AgentController AgentController; + private float steeringSpeed = 2.0f; + private void Start() { AgentController = GetComponentInParent(); @@ -58,6 +60,12 @@ private void Start() controls.Vehicle.IndicatorHazard.performed += IndicatorHazardPerformed; controls.Vehicle.FogLights.performed += FogLightsPerformed; controls.Vehicle.InteriorLight.performed += InteriorLightPerformed; + controls.Vehicle.ControllerSteering.started += ControllerSteeringStarted; + controls.Vehicle.ControllerSteering.performed += ControllerSteeringPerformed; + controls.Vehicle.ControllerSteering.canceled += ControllerSteeringCanceled; + controls.Vehicle.ControllerAcceleration.started += ControllerAccelerationStarted; + controls.Vehicle.ControllerAcceleration.performed += ControllerAccelerationPerformed; + controls.Vehicle.ControllerAcceleration.canceled += ControllerAccelerationCanceled; } } @@ -126,6 +134,36 @@ private void InteriorLightPerformed(UnityEngine.InputSystem.InputAction.Callback actions.InteriorLight = !actions.InteriorLight; } + private void ControllerSteeringStarted(UnityEngine.InputSystem.InputAction.CallbackContext obj) + { + keyboardInput.x = obj.ReadValue().x; + } + + private void ControllerSteeringPerformed(UnityEngine.InputSystem.InputAction.CallbackContext obj) + { + keyboardInput.x = obj.ReadValue().x; + } + + private void ControllerSteeringCanceled(UnityEngine.InputSystem.InputAction.CallbackContext obj) + { + keyboardInput.x = 0.0f; + } + + private void ControllerAccelerationStarted(UnityEngine.InputSystem.InputAction.CallbackContext obj) + { + keyboardInput.y = obj.ReadValue(); + } + + private void ControllerAccelerationPerformed(UnityEngine.InputSystem.InputAction.CallbackContext obj) + { + keyboardInput.y = obj.ReadValue(); + } + + private void ControllerAccelerationCanceled(UnityEngine.InputSystem.InputAction.CallbackContext obj) + { + keyboardInput.y = 0.0f; + } + private void Update() { if (SystemInfo.operatingSystemFamily == OperatingSystemFamily.Linux && Application.isEditor) @@ -154,7 +192,7 @@ private void Update() if (AgentController.Active) { - SteerInput = Mathf.MoveTowards(SteerInput, keyboardInput.x, Time.deltaTime); + SteerInput = Mathf.MoveTowards(SteerInput, keyboardInput.x, Time.deltaTime * steeringSpeed); AccelInput = keyboardInput.y; } } @@ -180,6 +218,12 @@ private void OnDestroy() controls.Vehicle.IndicatorHazard.performed -= IndicatorHazardPerformed; controls.Vehicle.FogLights.performed -= FogLightsPerformed; controls.Vehicle.InteriorLight.performed -= InteriorLightPerformed; + controls.Vehicle.ControllerSteering.started -= ControllerSteeringStarted; + controls.Vehicle.ControllerSteering.performed -= ControllerSteeringPerformed; + controls.Vehicle.ControllerSteering.canceled -= ControllerSteeringCanceled; + controls.Vehicle.ControllerAcceleration.started -= ControllerAccelerationStarted; + controls.Vehicle.ControllerAcceleration.performed -= ControllerAccelerationPerformed; + controls.Vehicle.ControllerAcceleration.canceled -= ControllerAccelerationCanceled; } } From 093de90646bac5a68e3a51a9c82b9b0130381ac2 Mon Sep 17 00:00:00 2001 From: Konstantin Kohl Date: Thu, 24 Oct 2019 10:43:44 +0200 Subject: [PATCH 2/2] Fixed missing comma in JSON, introduced by former merge --- Assets/Scripts/Controllers/Generated/SimulatorControls.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/Scripts/Controllers/Generated/SimulatorControls.cs b/Assets/Scripts/Controllers/Generated/SimulatorControls.cs index b8d905a5c..ddfb27e2c 100644 --- a/Assets/Scripts/Controllers/Generated/SimulatorControls.cs +++ b/Assets/Scripts/Controllers/Generated/SimulatorControls.cs @@ -612,7 +612,7 @@ public SimulatorControls() ""processors"": """", ""interactions"": """", ""bindings"": [] - } + }, { ""name"": ""CinematicNewPath"", ""id"": ""665b9005-f4bd-473b-9c46-4fd20642ac7d"",