170 lines
5.2 KiB
C#
170 lines
5.2 KiB
C#
#if USE_NEW_INPUT
|
|
using UnityEngine.InputSystem.OnScreen;
|
|
using UnityEngine.InputSystem.Layouts;
|
|
using UnityEngine.InputSystem;
|
|
#endif
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
using Invector.vCamera;
|
|
using Invector.vCharacterController;
|
|
using System;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class TouchPadOnScreen : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler
|
|
{
|
|
public static Action<bool> PointerDown;
|
|
|
|
[SerializeField] private float m_xMaxDelta = 2f;
|
|
[SerializeField] private float m_yMaxDelta = 2f;
|
|
[Space]
|
|
[SerializeField] private float m_xMaxDeltaEditor = 2f;
|
|
[SerializeField] private float m_yMaxDeltaEditor = 2f;
|
|
[Space]
|
|
[SerializeField] private float Xsensitivity = 1f;
|
|
[SerializeField] private float Ysensitivity = 1f;
|
|
[Space]
|
|
public float constDeltaPosition = 1f;
|
|
public bool useCameraMoveRotation;
|
|
public float thresholdX = 0.45f;
|
|
public float thresholdY = -0.30f;
|
|
|
|
private bool cameraMoveActive;
|
|
private readonly float XScreenSensitivity = 360f;
|
|
private float YScreenSensitivity = 125f;
|
|
private Vector2 m_startPoint;
|
|
private Vector2 m_deltaPosition;
|
|
private Vector2 m_currentPoint;
|
|
private Vector2 screenSize;
|
|
private vThirdPersonCamera vThirdPersonCamera;
|
|
private bool lockInput;
|
|
|
|
[Header("Debug info")]
|
|
[SerializeField] private float m_x;
|
|
[SerializeField] private float m_y;
|
|
[SerializeField, Range(-1f, 1f)] private float inputX;
|
|
[SerializeField, Range(-1f, 1f)] private float inputY;
|
|
|
|
private void Awake()
|
|
{
|
|
CutScene.CutSceneEnable += OnCutScene;
|
|
}
|
|
|
|
private void Start()
|
|
{
|
|
screenSize = new Vector2(Screen.width, Screen.height);
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
vThirdPersonCamera = vThirdPersonCamera.instance;
|
|
#if USE_NEW_INPUT
|
|
vInput.instance.inputActions.Player.Move.performed += Move_performed;
|
|
vInput.instance.inputActions.Player.Move.canceled += Move_canceled;
|
|
#endif
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
#if USE_NEW_INPUT
|
|
vInput.instance.inputActions.Player.Move.performed -= Move_performed;
|
|
vInput.instance.inputActions.Player.Move.canceled -= Move_canceled;
|
|
#endif
|
|
}
|
|
|
|
private void OnDestroy()
|
|
{
|
|
CutScene.CutSceneEnable -= OnCutScene;
|
|
}
|
|
|
|
private void OnCutScene(bool isLock)
|
|
{
|
|
lockInput = isLock;
|
|
m_x = 0f;
|
|
m_y = 0f;
|
|
}
|
|
#if USE_NEW_INPUT
|
|
|
|
private void Move_performed(InputAction.CallbackContext obj)
|
|
{
|
|
if (lockInput) return;
|
|
|
|
inputX = obj.ReadValue<Vector2>().x;
|
|
inputY = obj.ReadValue<Vector2>().y;
|
|
|
|
cameraMoveActive = true;
|
|
|
|
m_x = Mathf.Clamp((obj.ReadValue<Vector2>().x * constDeltaPosition * XScreenSensitivity * Xsensitivity) / screenSize.x, -m_xMaxDeltaEditor, m_xMaxDeltaEditor);
|
|
m_x = Mathf.Abs(obj.ReadValue<Vector2>().x) > thresholdX ? m_x : 0f;
|
|
m_x = obj.ReadValue<Vector2>().y > thresholdY ? m_x : -m_x;
|
|
m_y = 0f;
|
|
}
|
|
|
|
private void Move_canceled(InputAction.CallbackContext obj)
|
|
{
|
|
if (lockInput) return;
|
|
|
|
inputX = obj.ReadValue<Vector2>().x;
|
|
inputY = obj.ReadValue<Vector2>().y;
|
|
|
|
m_x = 0f;
|
|
m_y = 0f;
|
|
UpdateCameraRotation(m_x, m_y);
|
|
cameraMoveActive = false;
|
|
}
|
|
#endif
|
|
private void Update()
|
|
{
|
|
if (cameraMoveActive && useCameraMoveRotation)
|
|
{
|
|
UpdateCameraRotation(m_x, m_y);
|
|
}
|
|
}
|
|
|
|
public void OnPointerDown(PointerEventData eventData)
|
|
{
|
|
PointerDown?.Invoke(true);
|
|
|
|
m_startPoint = eventData.position;
|
|
m_currentPoint = eventData.position;
|
|
}
|
|
|
|
public void OnDrag(PointerEventData eventData)
|
|
{
|
|
m_startPoint = eventData.position;
|
|
|
|
m_deltaPosition = m_startPoint - m_currentPoint;
|
|
|
|
m_currentPoint = eventData.position;
|
|
|
|
#if UNITY_EDITOR
|
|
m_x = Mathf.Clamp((m_deltaPosition.x * XScreenSensitivity * Xsensitivity) / screenSize.x, -m_xMaxDeltaEditor, m_xMaxDeltaEditor);
|
|
m_y = Mathf.Clamp((m_deltaPosition.y * YScreenSensitivity * Ysensitivity) / screenSize.y, -m_yMaxDeltaEditor, m_yMaxDeltaEditor);
|
|
#else
|
|
m_x = Mathf.Clamp((m_deltaPosition.x * XScreenSensitivity * Xsensitivity) / screenSize.x, -m_xMaxDelta, m_xMaxDelta);
|
|
m_y = Mathf.Clamp((m_deltaPosition.y * YScreenSensitivity * Ysensitivity) / screenSize.y, -m_yMaxDelta, m_yMaxDelta);
|
|
#endif
|
|
|
|
|
|
UpdateCameraRotation(m_x, m_y);
|
|
}
|
|
|
|
public void OnPointerUp(PointerEventData eventData)
|
|
{
|
|
PointerDown?.Invoke(false);
|
|
m_x = 0f;
|
|
m_y = 0f;
|
|
}
|
|
|
|
private void UpdateCameraRotation(float x, float y)
|
|
{
|
|
if (vThirdPersonCamera)
|
|
{
|
|
vThirdPersonCamera.RotateCamera(x, y);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|