123 lines
2.4 KiB
C#
123 lines
2.4 KiB
C#
using System.Collections;
|
|
using UnityEngine;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
using UnityEngine.InputSystem;
|
|
#endif
|
|
|
|
namespace StarterAssets
|
|
{
|
|
public class StarterAssetsInputs : MonoBehaviour
|
|
{
|
|
[Header("Character Input Values")]
|
|
public Vector2 move;
|
|
public Vector2 look;
|
|
public bool jump;
|
|
public bool sprint;
|
|
public bool shoot;
|
|
public bool interact;
|
|
|
|
[Header("Movement Settings")]
|
|
public bool analogMovement;
|
|
|
|
[Header("Mouse Cursor Settings")]
|
|
public bool cursorLocked = true;
|
|
public bool cursorInputForLook = true;
|
|
|
|
public bool inputEnabled = true;
|
|
|
|
#if ENABLE_INPUT_SYSTEM
|
|
public void OnMove(InputValue value)
|
|
{
|
|
if(inputEnabled)
|
|
MoveInput(value.Get<Vector2>());
|
|
}
|
|
|
|
public void OnLook(InputValue value)
|
|
{
|
|
if(cursorInputForLook && inputEnabled)
|
|
{
|
|
LookInput(value.Get<Vector2>());
|
|
}
|
|
}
|
|
|
|
public void OnJump(InputValue value)
|
|
{
|
|
if(inputEnabled)
|
|
JumpInput(value.isPressed);
|
|
}
|
|
|
|
public void OnSprint(InputValue value)
|
|
{
|
|
if(inputEnabled)
|
|
SprintInput(value.isPressed);
|
|
}
|
|
public void OnShoot(InputValue value)
|
|
{
|
|
if(inputEnabled)
|
|
ShootInput(value.isPressed);
|
|
}
|
|
public void OnInteract(InputValue value)
|
|
{
|
|
if(inputEnabled)
|
|
InteractInput(value.isPressed);
|
|
}
|
|
#endif
|
|
|
|
|
|
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;
|
|
}
|
|
public void ShootInput(bool newShootState)
|
|
{
|
|
shoot = newShootState;
|
|
}
|
|
public void InteractInput(bool newInteractState)
|
|
{
|
|
interact = newInteractState;
|
|
}
|
|
|
|
private void OnApplicationFocus(bool hasFocus)
|
|
{
|
|
SetCursorState(cursorLocked);
|
|
}
|
|
|
|
private void SetCursorState(bool newState)
|
|
{
|
|
Cursor.lockState = newState ? CursorLockMode.Locked : CursorLockMode.None;
|
|
}
|
|
public void EnableInput()
|
|
{
|
|
inputEnabled = true;
|
|
}
|
|
public void DisableInput()
|
|
{
|
|
inputEnabled = false;
|
|
}
|
|
public IEnumerator ToggleInput(float delay)
|
|
{
|
|
inputEnabled = false; // Disable immediately
|
|
move = Vector2.zero;
|
|
look = Vector2.zero;
|
|
GetComponents<CharacterController>()[0].Move(Vector3.zero); // Prevents character from sliding
|
|
yield return new WaitForSeconds(delay);
|
|
inputEnabled = true; // Re-enable after delay
|
|
yield return null;
|
|
}
|
|
}
|
|
} |