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,
"name": "PlayerActions",
"name": "PlayerInputActions",
"maps": [
{
"name": "Player",

View File

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

View File

@ -108,15 +108,17 @@ MonoBehaviour:
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_ActiveFontFeatures: 00000000
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
@ -242,15 +244,17 @@ MonoBehaviour:
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_ActiveFontFeatures: 00000000
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
@ -353,6 +357,7 @@ GameObject:
- component: {fileID: 6949988666450315186}
- component: {fileID: 7471597901585849079}
- component: {fileID: 5443964135166462109}
- component: {fileID: 7653406841585074505}
m_Layer: 0
m_Name: PlayerPrefab
m_TagString: Untagged
@ -470,10 +475,10 @@ Rigidbody:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 480331571121010796}
serializedVersion: 4
serializedVersion: 5
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_LinearDamping: 0
m_AngularDamping: 0.05
m_CenterOfMass: {x: 0, y: 0, z: 0}
m_InertiaTensor: {x: 1, y: 1, z: 1}
m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1}
@ -1104,6 +1109,36 @@ MonoBehaviour:
interactionPrompt: {fileID: 1848602766038196315}
interactionIcon: {fileID: 6972307944780814254}
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
GameObject:
m_ObjectHideFlags: 0
@ -1542,6 +1577,7 @@ AudioSource:
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 0}
m_Resource: {fileID: 0}
m_PlayOnAwake: 0
m_Volume: 1
m_Pitch: 1
@ -1992,10 +2028,10 @@ Rigidbody:
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3639665566746515359}
serializedVersion: 4
serializedVersion: 5
m_Mass: 1
m_Drag: 0
m_AngularDrag: 0.05
m_LinearDamping: 0
m_AngularDamping: 0.05
m_CenterOfMass: {x: 0, y: 0, z: 0}
m_InertiaTensor: {x: 1, y: 1, z: 1}
m_InertiaRotation: {x: 0, y: 0, z: 0, w: 1}
@ -2874,6 +2910,7 @@ AudioSource:
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 0}
m_Resource: {fileID: 0}
m_PlayOnAwake: 0
m_Volume: 1
m_Pitch: 1
@ -3113,15 +3150,17 @@ MonoBehaviour:
m_lineSpacingMax: 0
m_paragraphSpacing: 0
m_charWidthMaxAdj: 0
m_enableWordWrapping: 1
m_TextWrappingMode: 1
m_wordWrappingRatios: 0.4
m_overflowMode: 0
m_linkedTextComponent: {fileID: 0}
parentLinkedComponent: {fileID: 0}
m_enableKerning: 1
m_ActiveFontFeatures: 6e72656b
m_enableExtraPadding: 0
checkPaddingRequired: 0
m_isRichText: 1
m_EmojiFallbackSupport: 1
m_parseCtrlCharacters: 1
m_isOrthographic: 1
m_isCullingEnabled: 0
@ -3218,6 +3257,7 @@ AudioSource:
serializedVersion: 4
OutputAudioMixerGroup: {fileID: 0}
m_audioClip: {fileID: 0}
m_Resource: {fileID: 0}
m_PlayOnAwake: 0
m_Volume: 1
m_Pitch: 1
@ -5279,6 +5319,10 @@ PrefabInstance:
propertyPath: m_hasFontAssetChanged
value: 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}
propertyPath: m_SizeDelta.y
value: 50

View File

@ -1,5 +1,6 @@
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.InputSystem;
public enum MovementType
{
@ -17,12 +18,19 @@ public class PlayerMovement : MonoBehaviour
[SerializeField] private MovementType movementType = MovementType.DirectionalInput;
[SerializeField] private BoolSharedField directionalInputMode;
public BoolSharedField DirectionalInputMode => directionalInputMode;
[Header("Controller Settings")]
[SerializeField] private float gamepadDeadzone = 0.2f;
[Header("Debug Visualization")]
[SerializeField] private bool showDebugVisuals = false;
// Input System variables
private PlayerInput playerInput;
private Vector2 moveInput;
private Vector2 aimInput;
private bool isMouseAiming;
private Transform target;
private NavMeshAgent agent;
private Vector3 direction = Vector3.zero;
@ -66,26 +74,56 @@ public class PlayerMovement : MonoBehaviour
startingAgentSpeed = agent.speed;
startingAgentAngularSpeed = agent.angularSpeed;
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()
{
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()
{
SetMovementType(directionalInputMode.Value ? MovementType.DirectionalInput : MovementType.PointAndClick);
if (movementType == MovementType.DirectionalInput)
{
DetectInputMethod();
// Handle movement first
// Handle movement
HandleDirectionalMovement();
// Then handle aiming, with mouse taking priority
// Handle aiming based on input method
if (!isUsingGamepad)
{
HandleMouseAiming();
@ -93,7 +131,6 @@ public class PlayerMovement : MonoBehaviour
else
{
HandleGamepadAiming();
hasMouseAimPoint = false; // Reset mouse aim when using gamepad
}
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()
{
if (agent.isStopped) return;
currentMovementDirection = new Vector2(
Input.GetAxisRaw(GameConstants.Input.HorizontalAxis),
Input.GetAxisRaw(GameConstants.Input.VerticalAxis));
Vector3 cameraForward = mainCamera.transform.forward;
Vector3 cameraRight = mainCamera.transform.right;
cameraForward.y = 0f;
@ -179,25 +188,8 @@ public class PlayerMovement : MonoBehaviour
}
}
private void HandleAimingRotation()
{
if (isUsingGamepad)
{
HandleGamepadAiming();
}
else
{
HandleMouseAiming();
}
}
private void HandleGamepadAiming()
{
currentAimDirection = new Vector2(
Input.GetAxisRaw(GameConstants.Input.AimHorizontalAxis),
Input.GetAxisRaw(GameConstants.Input.AimVerticalAxis)
);
if (currentAimDirection.magnitude > gamepadDeadzone)
{
Vector3 cameraForward = mainCamera.transform.forward;
@ -217,8 +209,6 @@ public class PlayerMovement : MonoBehaviour
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)
{
Quaternion targetRotation = Quaternion.LookRotation(lastAimDirection);
@ -236,13 +226,15 @@ public class PlayerMovement : MonoBehaviour
relativeMovementDirection.y = Vector2.Dot(lastAimDirection2, currentMovementDirection);
// Calculate strafe movement relative to aim direction
// Cross product for left/right movement
relativeMovementDirection.x = lastAimDirection2.x * currentMovementDirection.y -
lastAimDirection2.y * currentMovementDirection.x;
}
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))
{
@ -257,7 +249,6 @@ public class PlayerMovement : MonoBehaviour
lastAimDirection2.x = aimDirection.x;
lastAimDirection2.y = aimDirection.z;
// Always update rotation when using mouse
transform.rotation = Quaternion.LookRotation(aimDirection);
}
}
@ -330,6 +321,7 @@ public class PlayerMovement : MonoBehaviour
if (target != null)
FaceTarget();
}
public void FacePoint(Vector3 point)
{
direction = (point - transform.position).normalized;
@ -337,6 +329,7 @@ public class PlayerMovement : MonoBehaviour
lookRotation = Quaternion.LookRotation(direction);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * lookSpeed);
}
public void FaceTarget()
{
if (target == null) return;