new input system movement

This commit is contained in:
Vova
2024-01-04 08:48:04 +02:00
parent 95b2e13de8
commit 72c24ac8a7
6 changed files with 339 additions and 103 deletions
+211
View File
@@ -0,0 +1,211 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was auto-generated by com.unity.inputsystem:InputActionCodeGenerator
// version 1.7.0
// from Assets/Scripts/InputActions.inputactions
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Utilities;
public partial class @InputActions: IInputActionCollection2, IDisposable
{
public InputActionAsset asset { get; }
public @InputActions()
{
asset = InputActionAsset.FromJson(@"{
""name"": ""InputActions"",
""maps"": [
{
""name"": ""Player"",
""id"": ""a9631db6-afdf-47e4-a8a3-d63ab06f361c"",
""actions"": [
{
""name"": ""Movement"",
""type"": ""Value"",
""id"": ""bc1a3c6a-5454-4e22-a4c4-e507bd5ef719"",
""expectedControlType"": ""Vector2"",
""processors"": """",
""interactions"": """",
""initialStateCheck"": true
}
],
""bindings"": [
{
""name"": ""2D Vector"",
""id"": ""ec20865d-29b5-4923-a398-026e3b6a452a"",
""path"": ""2DVector"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": true,
""isPartOfComposite"": false
},
{
""name"": ""up"",
""id"": ""ec9e58f5-3e87-4b8e-98a0-d6f57dee5a62"",
""path"": ""<Keyboard>/upArrow"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""down"",
""id"": ""6833e932-d60c-49ad-84eb-2f63e3847d53"",
""path"": ""<Keyboard>/downArrow"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""left"",
""id"": ""aee692a9-6d5f-4fea-b555-8dc6c147efbd"",
""path"": ""<Keyboard>/leftArrow"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
},
{
""name"": ""right"",
""id"": ""ba2ffdd7-4da3-4b20-9b49-14448c0a8a15"",
""path"": ""<Keyboard>/rightArrow"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Movement"",
""isComposite"": false,
""isPartOfComposite"": true
}
]
}
],
""controlSchemes"": []
}");
// Player
m_Player = asset.FindActionMap("Player", throwIfNotFound: true);
m_Player_Movement = m_Player.FindAction("Movement", throwIfNotFound: true);
}
public void Dispose()
{
UnityEngine.Object.Destroy(asset);
}
public InputBinding? bindingMask
{
get => asset.bindingMask;
set => asset.bindingMask = value;
}
public ReadOnlyArray<InputDevice>? devices
{
get => asset.devices;
set => asset.devices = value;
}
public ReadOnlyArray<InputControlScheme> controlSchemes => asset.controlSchemes;
public bool Contains(InputAction action)
{
return asset.Contains(action);
}
public IEnumerator<InputAction> GetEnumerator()
{
return asset.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Enable()
{
asset.Enable();
}
public void Disable()
{
asset.Disable();
}
public IEnumerable<InputBinding> bindings => asset.bindings;
public InputAction FindAction(string actionNameOrId, bool throwIfNotFound = false)
{
return asset.FindAction(actionNameOrId, throwIfNotFound);
}
public int FindBinding(InputBinding bindingMask, out InputAction action)
{
return asset.FindBinding(bindingMask, out action);
}
// Player
private readonly InputActionMap m_Player;
private List<IPlayerActions> m_PlayerActionsCallbackInterfaces = new List<IPlayerActions>();
private readonly InputAction m_Player_Movement;
public struct PlayerActions
{
private @InputActions m_Wrapper;
public PlayerActions(@InputActions wrapper) { m_Wrapper = wrapper; }
public InputAction @Movement => m_Wrapper.m_Player_Movement;
public InputActionMap Get() { return m_Wrapper.m_Player; }
public void Enable() { Get().Enable(); }
public void Disable() { Get().Disable(); }
public bool enabled => Get().enabled;
public static implicit operator InputActionMap(PlayerActions set) { return set.Get(); }
public void AddCallbacks(IPlayerActions instance)
{
if (instance == null || m_Wrapper.m_PlayerActionsCallbackInterfaces.Contains(instance)) return;
m_Wrapper.m_PlayerActionsCallbackInterfaces.Add(instance);
@Movement.started += instance.OnMovement;
@Movement.performed += instance.OnMovement;
@Movement.canceled += instance.OnMovement;
}
private void UnregisterCallbacks(IPlayerActions instance)
{
@Movement.started -= instance.OnMovement;
@Movement.performed -= instance.OnMovement;
@Movement.canceled -= instance.OnMovement;
}
public void RemoveCallbacks(IPlayerActions instance)
{
if (m_Wrapper.m_PlayerActionsCallbackInterfaces.Remove(instance))
UnregisterCallbacks(instance);
}
public void SetCallbacks(IPlayerActions instance)
{
foreach (var item in m_Wrapper.m_PlayerActionsCallbackInterfaces)
UnregisterCallbacks(item);
m_Wrapper.m_PlayerActionsCallbackInterfaces.Clear();
AddCallbacks(instance);
}
}
public PlayerActions @Player => new PlayerActions(this);
public interface IPlayerActions
{
void OnMovement(InputAction.CallbackContext context);
}
}
+2
View File
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 18dbff63b01a846438c203369e377e9a
@@ -1,12 +1,12 @@
{
"name": "Controls",
"name": "InputActions",
"maps": [
{
"name": "player",
"name": "Player",
"id": "a9631db6-afdf-47e4-a8a3-d63ab06f361c",
"actions": [
{
"name": "New action",
"name": "Movement",
"type": "Value",
"id": "bc1a3c6a-5454-4e22-a4c4-e507bd5ef719",
"expectedControlType": "Vector2",
@@ -23,7 +23,7 @@
"interactions": "",
"processors": "",
"groups": "",
"action": "New action",
"action": "Movement",
"isComposite": true,
"isPartOfComposite": false
},
@@ -34,7 +34,7 @@
"interactions": "",
"processors": "",
"groups": "",
"action": "New action",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
},
@@ -45,7 +45,7 @@
"interactions": "",
"processors": "",
"groups": "",
"action": "New action",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
},
@@ -56,7 +56,7 @@
"interactions": "",
"processors": "",
"groups": "",
"action": "New action",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
},
@@ -67,7 +67,7 @@
"interactions": "",
"processors": "",
"groups": "",
"action": "New action",
"action": "Movement",
"isComposite": false,
"isPartOfComposite": true
}
@@ -8,7 +8,7 @@ ScriptedImporter:
assetBundleName:
assetBundleVariant:
script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3}
generateWrapperCode: 0
generateWrapperCode: 1
wrapperCodePath:
wrapperClassName:
wrapperCodeNamespace:
+5 -7
View File
@@ -1,9 +1,6 @@
using System;
using UnityEngine;
public class Player : Character
{
[SerializeField]
@@ -30,10 +27,11 @@ public class Player : Character
private GameObject _hammer;
private bool _isHoldingHammer = true;
public event EventHandler<TreasureType> OnPlayerTakeItem;
public InputActions _inputActions = new InputActions();
private void Awake()
{
if (Instance != null)
@@ -45,8 +43,7 @@ public class Player : Character
PlayerPrefs.SetString("lastExitName", string.Empty);
Instance = this;
DontDestroyOnLoad(gameObject);
_inputActions.Enable();
}
public void AddCoin()
@@ -88,7 +85,8 @@ public class Player : Character
_animator.SetTrigger("Body_ThrowHammer");
}
}
base.MoveTo(Input.GetAxisRaw("Horizontal"), isAllowVertical? Input.GetAxisRaw("Vertical"):0);
var move=_inputActions.Player.Movement.ReadValue<Vector2>();
base.MoveTo(move.x, isAllowVertical? move.y:0);
}
public void ThrowHammerObject()