Skip to content

Commit

Permalink
Add support of input system v2 (Unity-Technologies#2042)
Browse files Browse the repository at this point in the history
* Added support of new input system in FreeCamera.cs

* Added debug menu controls

* Added support for FreeCamera, mouse debug position and LookWithMouse

* Support input system in player movements

* Added input system suppoort to SimpleCameraController.cs

* Started to add input system V2 support in VFX graph

* Added guards for VFX events not compatible with new input system

* Added suport of new input system in VFX Mouse event binder

* Updated changelogs

* Cleanup

* Replaced ENABLE_INPUT_SYSTEM_PACKAGE by VFX_USE_INPUT_SYSTEM_PACKAGE

* Added link to 10.x what's new docs and added input system package support entry to 10.x what's new

* Update FreeCamera.cs

Co-authored-by: Lewis Jordan <[email protected]>
Co-authored-by: sebastienlagarde <[email protected]>
  • Loading branch information
3 people authored Oct 4, 2020
1 parent 1004653 commit a757f85
Show file tree
Hide file tree
Showing 20 changed files with 820 additions and 88 deletions.
3 changes: 3 additions & 0 deletions com.unity.render-pipelines.core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
Version Updated
The version number for this package has increased due to a version update of a related graphics package.

### Added
- Added the support of input system V2

### Fixed
- Fixed the scene view to scale correctly when hardware dynamic resolution is enabled (case 1158661)
- Fixed game view artifacts on resizing when hardware dynamic resolution was enabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,12 +381,6 @@ void OnGUI()
DebugManager.instance.Reset();
GUILayout.EndHorizontal();

// We check if the legacy input manager is not here because we can have both the new and old input system at the same time
// and in this case the debug menu works correctly.
#if !ENABLE_LEGACY_INPUT_MANAGER
EditorGUILayout.HelpBox("The debug menu does not support the new Unity Input package yet. inputs will be disabled in play mode and build.", MessageType.Error);
#endif

using (new EditorGUILayout.HorizontalScope())
{
// Side bar
Expand Down
118 changes: 101 additions & 17 deletions com.unity.render-pipelines.core/Runtime/Camera/FreeCamera.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
#define USE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif

using System.Collections.Generic;
using UnityEngine;

Expand All @@ -7,7 +12,6 @@ namespace UnityEngine.Rendering
/// Utility Free Camera component.
/// </summary>
[HelpURL(Documentation.baseURL + Documentation.version + Documentation.subURL + "Free-Camera" + Documentation.endURL)]
[ExecuteAlways]
public class FreeCamera : MonoBehaviour
{
/// <summary>
Expand Down Expand Up @@ -41,21 +45,66 @@ public class FreeCamera : MonoBehaviour
private static string kYAxis = "YAxis";
private static string kSpeedAxis = "Speed Axis";

#if USE_INPUT_SYSTEM
InputAction lookAction;
InputAction moveAction;
InputAction speedAction;
InputAction fireAction;
InputAction yMoveAction;
#endif

void OnEnable()
{
RegisterInputs();
}

void RegisterInputs()
{
#if UNITY_EDITOR
#if USE_INPUT_SYSTEM
var map = new InputActionMap("Free Camera");

lookAction = map.AddAction("look", binding: "<Mouse>/delta");
moveAction = map.AddAction("move", binding: "<Gamepad>/leftStick");
speedAction = map.AddAction("speed", binding: "<Gamepad>/dpad");
yMoveAction = map.AddAction("yMove");

lookAction.AddBinding("<Gamepad>/rightStick").WithProcessor("scaleVector2(x=15, y=15)");
moveAction.AddCompositeBinding("Dpad")
.With("Up", "<Keyboard>/w")
.With("Up", "<Keyboard>/upArrow")
.With("Down", "<Keyboard>/s")
.With("Down", "<Keyboard>/downArrow")
.With("Left", "<Keyboard>/a")
.With("Left", "<Keyboard>/leftArrow")
.With("Right", "<Keyboard>/d")
.With("Right", "<Keyboard>/rightArrow");
speedAction.AddCompositeBinding("Dpad")
.With("Up", "<Keyboard>/home")
.With("Down", "<Keyboard>/end");
yMoveAction.AddCompositeBinding("Dpad")
.With("Up", "<Keyboard>/pageUp")
.With("Down", "<Keyboard>/pageDown")
.With("Up", "<Keyboard>/e")
.With("Down", "<Keyboard>/q")
.With("Up", "<Gamepad>/rightshoulder")
.With("Down", "<Gamepad>/leftshoulder");

moveAction.Enable();
lookAction.Enable();
speedAction.Enable();
fireAction.Enable();
yMoveAction.Enable();
#endif

#if UNITY_EDITOR && !USE_INPUT_SYSTEM
List<InputManagerEntry> inputEntries = new List<InputManagerEntry>();

// Add new bindings
inputEntries.Add(new InputManagerEntry { name = kRightStickX, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fourth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f });
inputEntries.Add(new InputManagerEntry { name = kRightStickY, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Fifth, sensitivity = 1.0f, gravity = 1.0f, deadZone = 0.2f, invert = true });

inputEntries.Add(new InputManagerEntry { name = kYAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page up", altBtnPositive = "joystick button 5", btnNegative = "page down", altBtnNegative = "joystick button 4", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
inputEntries.Add(new InputManagerEntry { name = kYAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "q", btnNegative = "e", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });

inputEntries.Add(new InputManagerEntry { name = kSpeedAxis, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "home", btnNegative = "end", gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
inputEntries.Add(new InputManagerEntry { name = kSpeedAxis, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Seventh, gravity = 1000.0f, deadZone = 0.001f, sensitivity = 1000.0f });
Expand All @@ -64,33 +113,68 @@ void RegisterInputs()
#endif
}

void Update()
{
// If the debug menu is running, we don't want to conflict with its inputs.
if (DebugManager.instance.displayRuntimeUI)
return;
float inputRotateAxisX, inputRotateAxisY;
float inputChangeSpeed;
float inputVertical, inputHorizontal, inputYAxis;
bool leftShiftBoost, leftShift, fire1;

float inputRotateAxisX = 0.0f;
float inputRotateAxisY = 0.0f;
void UpdateInputs()
{
inputRotateAxisX = 0.0f;
inputRotateAxisY = 0.0f;
leftShiftBoost = false;
fire1 = false;

#if USE_INPUT_SYSTEM
var lookDelta = lookAction.ReadValue<Vector2>();
inputRotateAxisX = lookDelta.x * m_LookSpeedMouse * Time.deltaTime;
inputRotateAxisY = lookDelta.y * m_LookSpeedMouse * Time.deltaTime;

leftShift = Keyboard.current.leftShiftKey.isPressed;
fire1 = Mouse.current?.leftButton?.isPressed == true || Gamepad.current?.xButton?.isPressed == true;

inputChangeSpeed = speedAction.ReadValue<Vector2>().y;

var moveDelta = moveAction.ReadValue<Vector2>();
inputVertical = moveDelta.y;
inputHorizontal = moveDelta.x;
inputYAxis = yMoveAction.ReadValue<Vector2>().y;
#else
if (Input.GetMouseButton(1))
{
leftShiftBoost = true;
inputRotateAxisX = Input.GetAxis(kMouseX) * m_LookSpeedMouse;
inputRotateAxisY = Input.GetAxis(kMouseY) * m_LookSpeedMouse;
}
inputRotateAxisX += (Input.GetAxis(kRightStickX) * m_LookSpeedController * Time.deltaTime);
inputRotateAxisY += (Input.GetAxis(kRightStickY) * m_LookSpeedController * Time.deltaTime);

float inputChangeSpeed = Input.GetAxis(kSpeedAxis);
leftShift = Input.GetKeyDown(KeyCode.LeftShift);
fire1 = Input.GetAxis("Fire1") > 0.0f;
Debug.Log(fire1);

inputChangeSpeed = Input.GetAxis(kSpeedAxis);

inputVertical = Input.GetAxis(kVertical);
inputHorizontal = Input.GetAxis(kHorizontal);
inputYAxis = Input.GetAxis(kYAxis);
#endif
}

void Update()
{
// If the debug menu is running, we don't want to conflict with its inputs.
if (DebugManager.instance.displayRuntimeUI)
return;

UpdateInputs();

if (inputChangeSpeed != 0.0f)
{
m_MoveSpeed += inputChangeSpeed * m_MoveSpeedIncrement;
if (m_MoveSpeed < m_MoveSpeedIncrement) m_MoveSpeed = m_MoveSpeedIncrement;
}

float inputVertical = Input.GetAxis(kVertical);
float inputHorizontal = Input.GetAxis(kHorizontal);
float inputYAxis = Input.GetAxis(kYAxis);

bool moved = inputRotateAxisX != 0.0f || inputRotateAxisY != 0.0f || inputVertical != 0.0f || inputHorizontal != 0.0f || inputYAxis != 0.0f;
if (moved)
{
Expand All @@ -107,10 +191,10 @@ void Update()
transform.localRotation = Quaternion.Euler(newRotationX, newRotationY, transform.localEulerAngles.z);

float moveSpeed = Time.deltaTime * m_MoveSpeed;
if (Input.GetMouseButton(1))
moveSpeed *= Input.GetKey(KeyCode.LeftShift) ? m_Turbo : 1.0f;
if (leftShiftBoost)
moveSpeed *= leftShift ? m_Turbo : 1.0f;
else
moveSpeed *= Input.GetAxis("Fire1") > 0.0f ? m_Turbo : 1.0f;
moveSpeed *= fire1 ? m_Turbo : 1.0f;
transform.position += transform.forward * moveSpeed * inputVertical;
transform.position += transform.right * moveSpeed * inputHorizontal;
transform.position += Vector3.up * moveSpeed * inputYAxis;
Expand Down
Loading

0 comments on commit a757f85

Please sign in to comment.