Skip to content

Commit

Permalink
Merge pull request #10 from eviltwo/use-inputplayer
Browse files Browse the repository at this point in the history
Change to use PlayerInput for character and camera.
  • Loading branch information
eviltwo authored Jul 6, 2024
2 parents 6a42536 + 2228b80 commit d482a84
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 161 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
- 入力処理はInputSystemに対応しています。

# パッケージ一覧 (UPMでインポートできます)
### CharacterControls v0.8.0
### CharacterControls v0.9.0
キャラクターの歩行・ジャンプ。
```
https://github.com/eviltwo/ActionGameCore.git?path=src/ActionGameCore/Assets/CharacterControls
```

### CameraControls v1.5.0
### CameraControls v1.6.0
FPSとTPS視点のカメラ。
```
https://github.com/eviltwo/ActionGameCore.git?path=src/ActionGameCore/Assets/CameraControls
Expand Down
10 changes: 3 additions & 7 deletions src/ActionGameCore/Assets/CameraControls/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
# Changelog

## [1.5.0] - 2024-07-02
### Removed
- Remove SteamInputAPI.
## [1.6.0] - 2024-07-07
### Changed
- Changed to use PlayerInput component

## [1.4.0] - 2024-06-28
### Changed
- Merged with TPS package.
- Renamed namespace to CameraControls.
- Separated script to Controller and Input.

## [1.3.0] - 2024-06-28
### Changed
- Support SteamInput.

## [1.2.0] - 2024-05-16
### Changed
- Match camera sensitivity of mouse and controller.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [
{
"name": "com.unity.inputsystem",
"expression": "",
"define": "SUPPORT_INPUTSYSTEM"
}
],
"versionDefines": [],
"noEngineReferences": false
}
Original file line number Diff line number Diff line change
@@ -1,44 +1,62 @@
using System.Collections.Generic;
#if ENABLE_INPUT_SYSTEM
using CameraControls.Controllers;
using UnityEngine;

#if SUPPORT_INPUTSYSTEM

using UnityEngine.InputSystem;
#endif


namespace CameraControls.Inputs
{
public class CameraInput : MonoBehaviour
{
#if SUPPORT_INPUTSYSTEM
[Header("InputSystem")]
[SerializeField]
private InputActionReference _deltaActionReference = null;
private PlayerInput _playerInput = null;

[SerializeField]
private InputActionReference _continuousActionreference = null;
#endif
private InputActionReference _lookActionReference = null;

private ICameraController _cameraController;
private List<ICameraController> _cameraControllerBuffer = new List<ICameraController>();
private Vector2 _deltaPixels;
private Vector2 _analogValues;

private void Start()
private void Awake()
{
_cameraController = GetComponent<ICameraController>();
_playerInput.onActionTriggered += OnActionTriggerd;
}

#if SUPPORT_INPUTSYSTEM
_deltaActionReference?.action.Enable();
_continuousActionreference?.action.Enable();
#endif
private void OnDestroy()
{
_playerInput.onActionTriggered -= OnActionTriggerd;
}

private void OnActionTriggerd(InputAction.CallbackContext context)
{
if (_lookActionReference != null && context.action.name == _lookActionReference.action.name)
{
OnLook(context);
}
}

private void OnLook(InputAction.CallbackContext context)
{
if (context.action.activeControl.device is Pointer)
{
_deltaPixels = context.ReadValue<Vector2>();
}
else
{
_analogValues = context.ReadValue<Vector2>();
}
}

private void Update()
{
if (_cameraController == null || !_cameraController.Enabled)
{
GetComponents(_cameraControllerBuffer);
foreach (var controller in _cameraControllerBuffer)
var cameraControllers = GetComponents<ICameraController>();
foreach (var controller in cameraControllers)
{
if (controller.Enabled)
{
Expand All @@ -50,56 +68,24 @@ private void Update()
}

var deltaAngle = Vector2.zero;

// Digital input
{
var deltaPixels = GetDeltaPixels();
const float DpiAverage = 96;
var dpi = Screen.dpi == 0 ? DpiAverage : Screen.dpi;
const float InchForTurn = 13;
var v = deltaPixels / dpi / InchForTurn * 180;
deltaAngle = v.sqrMagnitude > deltaAngle.sqrMagnitude ? v : deltaAngle;
}

// Analog input
{
var analogValues = GetAnalogValues();
const float SecondsForTurn = 1.0f;
var v = analogValues * Time.deltaTime / SecondsForTurn * 180;
deltaAngle = v.sqrMagnitude > deltaAngle.sqrMagnitude ? v : deltaAngle;
}

deltaAngle += PixelToAngle(_deltaPixels);
deltaAngle += AnalogToAngle(_analogValues, Time.deltaTime);
_cameraController.SetDeltaAngles(deltaAngle);
}

private Vector2 GetDeltaPixels()
private static Vector2 PixelToAngle(Vector2 pixels)
{
var value = Vector2.zero;

#if SUPPORT_INPUTSYSTEM
if (_deltaActionReference != null)
{
var v = _deltaActionReference.action.ReadValue<Vector2>();
value = v.sqrMagnitude > value.sqrMagnitude ? v : value;
}
#endif

return value;
const float DpiAverage = 96;
var dpi = Screen.dpi == 0 ? DpiAverage : Screen.dpi;
const float InchForTurn = 13;
return pixels / dpi / InchForTurn * 180;
}

private Vector2 GetAnalogValues()
private static Vector2 AnalogToAngle(Vector2 analog, float deltaTime)
{
var value = Vector2.zero;

#if SUPPORT_INPUTSYSTEM
if (_continuousActionreference != null)
{
var v = _continuousActionreference.action.ReadValue<Vector2>();
value = v.sqrMagnitude > value.sqrMagnitude ? v : value;
}
#endif

return value;
const float SecondsForTurn = 1.0f;
return analog * Time.deltaTime / SecondsForTurn * 180;
}
}
}
#endif
2 changes: 1 addition & 1 deletion src/ActionGameCore/Assets/CameraControls/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.eviltwo.camera-controls",
"displayName": "Camera Controls",
"version": "1.5.0",
"version": "1.6.0",
"unity": "2022.3",
"description": "Camera movements for FPS and TPS.",
"author": {
Expand Down
8 changes: 2 additions & 6 deletions src/ActionGameCore/Assets/CharacterControls/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# Changelog

## [0.8.0] - 2024-07-02
### Removed
- Remove SteamInputAPI.

## [0.7.0] - 2024-06-28
## [0.9.0] - 2024-07-07
### Changed
- Support SteamInputAPI.
- Changed to use PlayerInput component

## [0.6.1] - 2024-03-06
### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
"precompiledReferences": [],
"autoReferenced": false,
"defineConstraints": [],
"versionDefines": [
{
"name": "com.unity.inputsystem",
"expression": "",
"define": "SUPPORT_INPUTSYSTEM"
}
],
"versionDefines": [],
"noEngineReferences": false
}
Original file line number Diff line number Diff line change
@@ -1,72 +1,60 @@
#if ENABLE_INPUT_SYSTEM
using CharacterControls.Movements;
using UnityEngine;

#if SUPPORT_INPUTSYSTEM
using UnityEngine.InputSystem;
#endif

namespace CharacterControls.Inputs
{
public class CharacterMoveInput : MonoBehaviour
{
#if SUPPORT_INPUTSYSTEM
[Header("InputSystem")]
[SerializeField]
private PlayerInput _playerInput = null;

[SerializeField]
private InputActionReference _moveActionReference = null;

[SerializeField]
private InputActionReference _jumpActionReference = null;
#endif

public IMoveController MoveController { get; set; }

private void Start()
private void Awake()
{
#if SUPPORT_INPUTSYSTEM
_moveActionReference?.action.Enable();
_jumpActionReference?.action.Enable();
MoveController = GetComponent<IMoveController>();
#endif
_playerInput.onActionTriggered += OnActionTriggerd;
}

private void Update()
private void OnDestroy()
{
if (MoveController == null)
{
return;
}

UpdateMoveInput();
UpdateJumpInput();
_playerInput.onActionTriggered -= OnActionTriggerd;
}

private void UpdateMoveInput()
private void OnActionTriggerd(InputAction.CallbackContext context)
{
var value = Vector2.zero;
if (_moveActionReference != null && context.action.name == _moveActionReference.action.name)
{
OnMove(context);
}

#if SUPPORT_INPUTSYSTEM
if (_moveActionReference != null)
if (_jumpActionReference != null && context.action.name == _jumpActionReference.action.name)
{
var v = _moveActionReference.action.ReadValue<Vector2>();
value = v.sqrMagnitude > value.sqrMagnitude ? v : value;
OnJump(context);
}
#endif
}

private void OnMove(InputAction.CallbackContext context)
{
var value = context.ReadValue<Vector2>();
MoveController.SetMoveInput(value);
}

private void UpdateJumpInput()
private void OnJump(InputAction.CallbackContext context)
{
var value = false;

#if SUPPORT_INPUTSYSTEM
value |= _jumpActionReference != null && _jumpActionReference.action.WasPressedThisFrame();
#endif

if (value)
if (context.performed && context.startTime > Time.realtimeSinceStartup - 0.5f)
{
MoveController.SetJumpInput(1);
}
}
}
}
#endif
2 changes: 1 addition & 1 deletion src/ActionGameCore/Assets/CharacterControls/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.eviltwo.character-controls",
"displayName": "Character Controls",
"version": "0.8.0",
"version": "0.9.0",
"unity": "2022.3",
"description": "Base movements for character of 3D action game.",
"author": {
Expand Down
Loading

0 comments on commit d482a84

Please sign in to comment.