Skip to content

Commit

Permalink
Add simple (and flawed) crouching
Browse files Browse the repository at this point in the history
  • Loading branch information
Fripe070 committed Jun 5, 2023
1 parent fc96fae commit f541909
Show file tree
Hide file tree
Showing 4 changed files with 439 additions and 83 deletions.
32 changes: 28 additions & 4 deletions Assets/InputSystem/StarterAssets.inputactions
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,44 @@
"id": "6bc1aaf4-b110-4ff7-891e-5b9fe6f32c4d",
"expectedControlType": "Vector2",
"processors": "",
"interactions": ""
"interactions": "",
"initialStateCheck": true
},
{
"name": "Look",
"type": "Value",
"id": "2690c379-f54d-45be-a724-414123833eb4",
"expectedControlType": "Vector2",
"processors": "",
"interactions": ""
"interactions": "",
"initialStateCheck": true
},
{
"name": "Jump",
"type": "Button",
"id": "8c4abdf8-4099-493a-aa1a-129acec7c3df",
"expectedControlType": "Button",
"processors": "",
"interactions": ""
"interactions": "",
"initialStateCheck": false
},
{
"name": "Sprint",
"type": "PassThrough",
"id": "980e881e-182c-404c-8cbf-3d09fdb48fef",
"expectedControlType": "",
"processors": "",
"interactions": ""
"interactions": "",
"initialStateCheck": false
},
{
"name": "Crouch",
"type": "PassThrough",
"id": "0590970e-5069-4716-9197-5b602bdbbf1e",
"expectedControlType": "",
"processors": "",
"interactions": "",
"initialStateCheck": false
}
],
"bindings": [
Expand Down Expand Up @@ -214,6 +227,17 @@
"action": "Sprint",
"isComposite": false,
"isPartOfComposite": false
},
{
"name": "",
"id": "4100e7f2-4b21-41e6-8d67-a58af857ec1b",
"path": "<Keyboard>/leftCtrl",
"interactions": "",
"processors": "",
"groups": "KeyboardMouse",
"action": "Crouch",
"isComposite": false,
"isPartOfComposite": false
}
]
}
Expand Down
142 changes: 73 additions & 69 deletions Assets/InputSystem/StarterAssetsInputs.cs
Original file line number Diff line number Diff line change
@@ -1,80 +1,84 @@
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif

namespace StarterAssets
namespace InputSystem
{
public class StarterAssetsInputs : MonoBehaviour
{
[Header("Character Input Values")]
public Vector2 move;
public Vector2 look;
public bool jump;
public bool sprint;
public class StarterAssetsInputs : MonoBehaviour
{
[Header("Character Input Values")] public Vector2 move;

public Vector2 look;
public bool jump;
public bool sprint;
public bool crouch;

[Header("Movement Settings")] public bool analogMovement;

[Header("Mouse Cursor Settings")] public bool cursorLocked = true;

public bool cursorInputForLook = true;

private void OnApplicationFocus(bool hasFocus)
{
SetCursorState(cursorLocked);
}


[Header("Movement Settings")]
public bool analogMovement;
public void MoveInput(Vector2 newMoveDirection)
{
move = newMoveDirection;
}

[Header("Mouse Cursor Settings")]
public bool cursorLocked = true;
public bool cursorInputForLook = true;
public void LookInput(Vector2 newLookDirection)
{
look = newLookDirection;
}

public void JumpInput(bool newJumpState)
{
jump = newJumpState;
}

public void SprintInput(bool newSprintState)
{
sprint = newSprintState;
}

public void CrouchInput(bool newCrouchState)
{
crouch = newCrouchState;
}

private void SetCursorState(bool newState)
{
Cursor.lockState = newState ? CursorLockMode.Locked : CursorLockMode.None;
}

#if ENABLE_INPUT_SYSTEM
public void OnMove(InputValue value)
{
MoveInput(value.Get<Vector2>());
}

public void OnLook(InputValue value)
{
if(cursorInputForLook)
{
LookInput(value.Get<Vector2>());
}
}

public void OnJump(InputValue value)
{
JumpInput(value.isPressed);
}

public void OnSprint(InputValue value)
{
SprintInput(value.isPressed);
}
#endif
public void OnMove(InputValue value)
{
MoveInput(value.Get<Vector2>());
}

public void OnLook(InputValue value)
{
if (cursorInputForLook) LookInput(value.Get<Vector2>());
}

public void OnJump(InputValue value)
{
JumpInput(value.isPressed);
}

public void MoveInput(Vector2 newMoveDirection)
{
move = newMoveDirection;
}

public void LookInput(Vector2 newLookDirection)
{
look = newLookDirection;
}

public void JumpInput(bool newJumpState)
{
jump = newJumpState;
}

public void SprintInput(bool newSprintState)
{
sprint = newSprintState;
}

private void OnApplicationFocus(bool hasFocus)
{
SetCursorState(cursorLocked);
}

private void SetCursorState(bool newState)
{
Cursor.lockState = newState ? CursorLockMode.Locked : CursorLockMode.None;
}
}

public void OnSprint(InputValue value)
{
SprintInput(value.isPressed);
}

public void OnCrouch(InputValue value)
{
CrouchInput(value.isPressed);
}
#endif
}
}
Loading

0 comments on commit f541909

Please sign in to comment.