CinemachineTouchInputMapper.cs 974 B

123456789101112131415161718192021222324252627282930313233
  1. using Cinemachine;
  2. using UnityEngine;
  3. /// <summary>
  4. /// This is an add-on behaviour that globally maps the touch control
  5. /// to standard input channels, such as mouse X and mouse Y.
  6. /// Drop it on any game object in your scene.
  7. /// </summary>
  8. public class CinemachineTouchInputMapper : MonoBehaviour
  9. {
  10. public float TouchSensitivityX = 10f;
  11. public float TouchSensitivityY = 10f;
  12. public string TouchXInputMapTo = "Mouse X";
  13. public string TouchYInputMapTo = "Mouse Y";
  14. void Start()
  15. {
  16. CinemachineCore.GetInputAxis = GetInputAxis;
  17. }
  18. private float GetInputAxis(string axisName)
  19. {
  20. if (Input.touchCount > 0)
  21. {
  22. if (axisName == TouchXInputMapTo)
  23. return Input.touches[0].deltaPosition.x / TouchSensitivityX;
  24. if (axisName == TouchYInputMapTo)
  25. return Input.touches[0].deltaPosition.y / TouchSensitivityY;
  26. }
  27. return Input.GetAxis(axisName);
  28. }
  29. }