player movement with new input system

This commit is contained in:
Pedro Gomes 2025-09-10 18:44:00 +01:00
parent 55d15a41b7
commit 7d756644ac
6 changed files with 2299 additions and 68 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: e366469444401d94bb910d94c9cf514e

View File

@ -1,6 +1,6 @@
{ {
"version": 1, "version": 1,
"name": "PlayerActions", "name": "PlayerInputActions",
"maps": [ "maps": [
{ {
"name": "Player", "name": "Player",

View File

@ -8,7 +8,7 @@ ScriptedImporter:
assetBundleName: assetBundleName:
assetBundleVariant: assetBundleVariant:
script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3} script: {fileID: 11500000, guid: 8404be70184654265930450def6a9037, type: 3}
generateWrapperCode: 0 generateWrapperCode: 1
wrapperCodePath: wrapperCodePath:
wrapperClassName: wrapperClassName:
wrapperCodeNamespace: wrapperCodeNamespace:

View File

@ -108,15 +108,17 @@ MonoBehaviour:
m_lineSpacingMax: 0 m_lineSpacingMax: 0
m_paragraphSpacing: 0 m_paragraphSpacing: 0
m_charWidthMaxAdj: 0 m_charWidthMaxAdj: 0
m_enableWordWrapping: 1 m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4 m_wordWrappingRatios: 0.4
m_overflowMode: 0 m_overflowMode: 0
m_linkedTextComponent: {fileID: 0} m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0} parentLinkedComponent: {fileID: 0}
m_enableKerning: 1 m_enableKerning: 1
m_ActiveFontFeatures: 00000000
m_enableExtraPadding: 0 m_enableExtraPadding: 0
checkPaddingRequired: 0 checkPaddingRequired: 0
m_isRichText: 1 m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1 m_parseCtrlCharacters: 1
m_isOrthographic: 1 m_isOrthographic: 1
m_isCullingEnabled: 0 m_isCullingEnabled: 0
@ -242,15 +244,17 @@ MonoBehaviour:
m_lineSpacingMax: 0 m_lineSpacingMax: 0
m_paragraphSpacing: 0 m_paragraphSpacing: 0
m_charWidthMaxAdj: 0 m_charWidthMaxAdj: 0
m_enableWordWrapping: 1 m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4 m_wordWrappingRatios: 0.4
m_overflowMode: 0 m_overflowMode: 0
m_linkedTextComponent: {fileID: 0} m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0} parentLinkedComponent: {fileID: 0}
m_enableKerning: 1 m_enableKerning: 1
m_ActiveFontFeatures: 00000000
m_enableExtraPadding: 0 m_enableExtraPadding: 0
checkPaddingRequired: 0 checkPaddingRequired: 0
m_isRichText: 1 m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1 m_parseCtrlCharacters: 1
m_isOrthographic: 1 m_isOrthographic: 1
m_isCullingEnabled: 0 m_isCullingEnabled: 0
@ -353,6 +357,7 @@ GameObject:
- component: {fileID: 6949988666450315186} - component: {fileID: 6949988666450315186}
- component: {fileID: 7471597901585849079} - component: {fileID: 7471597901585849079}
- component: {fileID: 5443964135166462109} - component: {fileID: 5443964135166462109}
- component: {fileID: 7653406841585074505}
m_Layer: 0 m_Layer: 0
m_Name: PlayerPrefab m_Name: PlayerPrefab
m_TagString: Untagged m_TagString: Untagged
@ -470,10 +475,10 @@ Rigidbody:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 480331571121010796} m_GameObject: {fileID: 480331571121010796}
serializedVersion: 4 serializedVersion: 5
m_Mass: 1 m_Mass: 1
m_Drag: 0 m_LinearDamping: 0
m_AngularDrag: 0.05 m_AngularDamping: 0.05
m_CenterOfMass: {x: 0, y: 0, z: 0} m_CenterOfMass: {x: 0, y: 0, z: 0}
m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaTensor: {x: 1, y: 1, z: 1}
m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1}
@ -1104,6 +1109,36 @@ MonoBehaviour:
interactionPrompt: {fileID: 1848602766038196315} interactionPrompt: {fileID: 1848602766038196315}
interactionIcon: {fileID: 6972307944780814254} interactionIcon: {fileID: 6972307944780814254}
interactionText: {fileID: 5287681643690581492} interactionText: {fileID: 5287681643690581492}
--- !u!114 &7653406841585074505
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 480331571121010796}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 62899f850307741f2a39c98a8b639597, type: 3}
m_Name:
m_EditorClassIdentifier: Unity.InputSystem::UnityEngine.InputSystem.PlayerInput
m_Actions: {fileID: -944628639613478452, guid: dd7c3a48e4544a541be43334e4b28863, type: 3}
m_NotificationBehavior: 0
m_UIInputModule: {fileID: 0}
m_DeviceLostEvent:
m_PersistentCalls:
m_Calls: []
m_DeviceRegainedEvent:
m_PersistentCalls:
m_Calls: []
m_ControlsChangedEvent:
m_PersistentCalls:
m_Calls: []
m_ActionEvents: []
m_NeverAutoSwitchControlSchemes: 0
m_DefaultControlScheme:
m_DefaultActionMap: Player
m_SplitScreenIndex: -1
m_Camera: {fileID: 0}
--- !u!1 &647488901503982106 --- !u!1 &647488901503982106
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1542,6 +1577,7 @@ AudioSource:
serializedVersion: 4 serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0} OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 0} m_audioClip: {fileID: 0}
m_Resource: {fileID: 0}
m_PlayOnAwake: 0 m_PlayOnAwake: 0
m_Volume: 1 m_Volume: 1
m_Pitch: 1 m_Pitch: 1
@ -1992,10 +2028,10 @@ Rigidbody:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3639665566746515359} m_GameObject: {fileID: 3639665566746515359}
serializedVersion: 4 serializedVersion: 5
m_Mass: 1 m_Mass: 1
m_Drag: 0 m_LinearDamping: 0
m_AngularDrag: 0.05 m_AngularDamping: 0.05
m_CenterOfMass: {x: 0, y: 0, z: 0} m_CenterOfMass: {x: 0, y: 0, z: 0}
m_InertiaTensor: {x: 1, y: 1, z: 1} m_InertiaTensor: {x: 1, y: 1, z: 1}
m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1} m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1}
@ -2874,6 +2910,7 @@ AudioSource:
serializedVersion: 4 serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0} OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 0} m_audioClip: {fileID: 0}
m_Resource: {fileID: 0}
m_PlayOnAwake: 0 m_PlayOnAwake: 0
m_Volume: 1 m_Volume: 1
m_Pitch: 1 m_Pitch: 1
@ -3113,15 +3150,17 @@ MonoBehaviour:
m_lineSpacingMax: 0 m_lineSpacingMax: 0
m_paragraphSpacing: 0 m_paragraphSpacing: 0
m_charWidthMaxAdj: 0 m_charWidthMaxAdj: 0
m_enableWordWrapping: 1 m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4 m_wordWrappingRatios: 0.4
m_overflowMode: 0 m_overflowMode: 0
m_linkedTextComponent: {fileID: 0} m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0} parentLinkedComponent: {fileID: 0}
m_enableKerning: 1 m_enableKerning: 1
m_ActiveFontFeatures: 6e72656b
m_enableExtraPadding: 0 m_enableExtraPadding: 0
checkPaddingRequired: 0 checkPaddingRequired: 0
m_isRichText: 1 m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1 m_parseCtrlCharacters: 1
m_isOrthographic: 1 m_isOrthographic: 1
m_isCullingEnabled: 0 m_isCullingEnabled: 0
@ -3218,6 +3257,7 @@ AudioSource:
serializedVersion: 4 serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0} OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 0} m_audioClip: {fileID: 0}
m_Resource: {fileID: 0}
m_PlayOnAwake: 0 m_PlayOnAwake: 0
m_Volume: 1 m_Volume: 1
m_Pitch: 1 m_Pitch: 1
@ -5279,6 +5319,10 @@ PrefabInstance:
propertyPath: m_hasFontAssetChanged propertyPath: m_hasFontAssetChanged
value: 0 value: 0
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 1063629724385205317, guid: 2e56f69e0872f744daa6c9080ecf3040, type: 3}
propertyPath: 'm_ActiveFontFeatures.Array.data[0]'
value: 1801810542
objectReference: {fileID: 0}
- target: {fileID: 1674092480321374837, guid: 2e56f69e0872f744daa6c9080ecf3040, type: 3} - target: {fileID: 1674092480321374837, guid: 2e56f69e0872f744daa6c9080ecf3040, type: 3}
propertyPath: m_SizeDelta.y propertyPath: m_SizeDelta.y
value: 50 value: 50

View File

@ -1,5 +1,6 @@
using UnityEngine; using UnityEngine;
using UnityEngine.AI; using UnityEngine.AI;
using UnityEngine.InputSystem;
public enum MovementType public enum MovementType
{ {
@ -17,12 +18,19 @@ public class PlayerMovement : MonoBehaviour
[SerializeField] private MovementType movementType = MovementType.DirectionalInput; [SerializeField] private MovementType movementType = MovementType.DirectionalInput;
[SerializeField] private BoolSharedField directionalInputMode; [SerializeField] private BoolSharedField directionalInputMode;
public BoolSharedField DirectionalInputMode => directionalInputMode; public BoolSharedField DirectionalInputMode => directionalInputMode;
[Header("Controller Settings")] [Header("Controller Settings")]
[SerializeField] private float gamepadDeadzone = 0.2f; [SerializeField] private float gamepadDeadzone = 0.2f;
[Header("Debug Visualization")] [Header("Debug Visualization")]
[SerializeField] private bool showDebugVisuals = false; [SerializeField] private bool showDebugVisuals = false;
// Input System variables
private PlayerInput playerInput;
private Vector2 moveInput;
private Vector2 aimInput;
private bool isMouseAiming;
private Transform target; private Transform target;
private NavMeshAgent agent; private NavMeshAgent agent;
private Vector3 direction = Vector3.zero; private Vector3 direction = Vector3.zero;
@ -36,7 +44,7 @@ public class PlayerMovement : MonoBehaviour
private ProjectileSpawnLocationController aimController; private ProjectileSpawnLocationController aimController;
private Vector3 currentAimPoint; private Vector3 currentAimPoint;
public Vector2 currentMovementDirection= new Vector2(); public Vector2 currentMovementDirection = new Vector2();
public Vector2 currentAimDirection = new Vector2(); public Vector2 currentAimDirection = new Vector2();
public Vector2 lastAimDirection2 = new Vector2(); public Vector2 lastAimDirection2 = new Vector2();
public Vector2 relativeMovementDirection = new Vector2(); public Vector2 relativeMovementDirection = new Vector2();
@ -66,26 +74,56 @@ public class PlayerMovement : MonoBehaviour
startingAgentSpeed = agent.speed; startingAgentSpeed = agent.speed;
startingAgentAngularSpeed = agent.angularSpeed; startingAgentAngularSpeed = agent.angularSpeed;
startingAgentAcceleration = agent.acceleration; startingAgentAcceleration = agent.acceleration;
// Initialize Input System
playerInput = GetComponent<PlayerInput>();
if (playerInput == null)
{
Debug.LogWarning("PlayerInput component not found. Add PlayerInput component to use Input System.");
}
} }
void Start() void Start()
{ {
SetupAgentForDirectionalMovement(); SetupAgentForDirectionalMovement();
} }
// Input callbacks - these will be called by Unity's SendMessage system
public void OnMove(InputValue value)
{
moveInput = value.Get<Vector2>();
currentMovementDirection = moveInput;
}
public void OnStick(InputValue value)
{
aimInput = value.Get<Vector2>();
currentAimDirection = aimInput;
// Detect gamepad usage
if (aimInput.magnitude > gamepadDeadzone)
{
isUsingGamepad = true;
hasMouseAimPoint = false;
}
}
public void OnLook(InputValue value)
{
// This gets called when mouse moves, indicating mouse usage
isUsingGamepad = false;
}
private void Update() private void Update()
{ {
SetMovementType(directionalInputMode.Value ? MovementType.DirectionalInput : MovementType.PointAndClick); SetMovementType(directionalInputMode.Value ? MovementType.DirectionalInput : MovementType.PointAndClick);
if (movementType == MovementType.DirectionalInput) if (movementType == MovementType.DirectionalInput)
{ {
DetectInputMethod(); // Handle movement
// Handle movement first
HandleDirectionalMovement(); HandleDirectionalMovement();
// Then handle aiming, with mouse taking priority // Handle aiming based on input method
if (!isUsingGamepad) if (!isUsingGamepad)
{ {
HandleMouseAiming(); HandleMouseAiming();
@ -93,7 +131,6 @@ public class PlayerMovement : MonoBehaviour
else else
{ {
HandleGamepadAiming(); HandleGamepadAiming();
hasMouseAimPoint = false; // Reset mouse aim when using gamepad
} }
HandleRelativeForAnimation(); HandleRelativeForAnimation();
@ -115,38 +152,10 @@ public class PlayerMovement : MonoBehaviour
} }
} }
private void DetectInputMethod()
{
Vector2 gamepadMove = new Vector2(
Input.GetAxisRaw(GameConstants.Input.HorizontalAxis),
Input.GetAxisRaw(GameConstants.Input.VerticalAxis));
Vector2 gamepadLook = new Vector2(
Input.GetAxisRaw(GameConstants.Input.AimHorizontalAxis),
Input.GetAxisRaw(GameConstants.Input.AimVerticalAxis));
// Check for mouse movement
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
isUsingGamepad = false;
return;
}
// Check for gamepad input
if (gamepadMove.magnitude > gamepadDeadzone || gamepadLook.magnitude > gamepadDeadzone)
{
isUsingGamepad = true;
hasMouseAimPoint = false; // Reset mouse aim when using gamepad
}
}
private void HandleDirectionalMovement() private void HandleDirectionalMovement()
{ {
if (agent.isStopped) return; if (agent.isStopped) return;
currentMovementDirection = new Vector2(
Input.GetAxisRaw(GameConstants.Input.HorizontalAxis),
Input.GetAxisRaw(GameConstants.Input.VerticalAxis));
Vector3 cameraForward = mainCamera.transform.forward; Vector3 cameraForward = mainCamera.transform.forward;
Vector3 cameraRight = mainCamera.transform.right; Vector3 cameraRight = mainCamera.transform.right;
cameraForward.y = 0f; cameraForward.y = 0f;
@ -179,25 +188,8 @@ public class PlayerMovement : MonoBehaviour
} }
} }
private void HandleAimingRotation()
{
if (isUsingGamepad)
{
HandleGamepadAiming();
}
else
{
HandleMouseAiming();
}
}
private void HandleGamepadAiming() private void HandleGamepadAiming()
{ {
currentAimDirection = new Vector2(
Input.GetAxisRaw(GameConstants.Input.AimHorizontalAxis),
Input.GetAxisRaw(GameConstants.Input.AimVerticalAxis)
);
if (currentAimDirection.magnitude > gamepadDeadzone) if (currentAimDirection.magnitude > gamepadDeadzone)
{ {
Vector3 cameraForward = mainCamera.transform.forward; Vector3 cameraForward = mainCamera.transform.forward;
@ -217,8 +209,6 @@ public class PlayerMovement : MonoBehaviour
gamepadRotationSpeed * Time.deltaTime gamepadRotationSpeed * Time.deltaTime
); );
} }
// Remove the else { isAiming = false; } - we want to maintain the last aim direction
// Instead, maintain the last valid aim direction
else if (lastAimDirection != Vector3.zero) else if (lastAimDirection != Vector3.zero)
{ {
Quaternion targetRotation = Quaternion.LookRotation(lastAimDirection); Quaternion targetRotation = Quaternion.LookRotation(lastAimDirection);
@ -236,13 +226,15 @@ public class PlayerMovement : MonoBehaviour
relativeMovementDirection.y = Vector2.Dot(lastAimDirection2, currentMovementDirection); relativeMovementDirection.y = Vector2.Dot(lastAimDirection2, currentMovementDirection);
// Calculate strafe movement relative to aim direction // Calculate strafe movement relative to aim direction
// Cross product for left/right movement
relativeMovementDirection.x = lastAimDirection2.x * currentMovementDirection.y - relativeMovementDirection.x = lastAimDirection2.x * currentMovementDirection.y -
lastAimDirection2.y * currentMovementDirection.x; lastAimDirection2.y * currentMovementDirection.x;
} }
private void HandleMouseAiming() private void HandleMouseAiming()
{ {
Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition); // Get the current mouse position directly from the Input System
Vector3 screenMousePosition = Mouse.current.position.ReadValue();
Ray ray = mainCamera.ScreenPointToRay(screenMousePosition);
if (groundPlane.Raycast(ray, out float distance)) if (groundPlane.Raycast(ray, out float distance))
{ {
@ -257,7 +249,6 @@ public class PlayerMovement : MonoBehaviour
lastAimDirection2.x = aimDirection.x; lastAimDirection2.x = aimDirection.x;
lastAimDirection2.y = aimDirection.z; lastAimDirection2.y = aimDirection.z;
// Always update rotation when using mouse
transform.rotation = Quaternion.LookRotation(aimDirection); transform.rotation = Quaternion.LookRotation(aimDirection);
} }
} }
@ -330,6 +321,7 @@ public class PlayerMovement : MonoBehaviour
if (target != null) if (target != null)
FaceTarget(); FaceTarget();
} }
public void FacePoint(Vector3 point) public void FacePoint(Vector3 point)
{ {
direction = (point - transform.position).normalized; direction = (point - transform.position).normalized;
@ -337,6 +329,7 @@ public class PlayerMovement : MonoBehaviour
lookRotation = Quaternion.LookRotation(direction); lookRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * lookSpeed); transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * lookSpeed);
} }
public void FaceTarget() public void FaceTarget()
{ {
if (target == null) return; if (target == null) return;