Advertisement
HashZayed

Untitled

Nov 13th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Scrub : MonoBehaviour {
  6. public Animation anim;
  7. [Range(0.1f,10f)]
  8. public float inertia = 1.0f;
  9. float maxScrubVal = 1;
  10. float scrubVal = 0;
  11.  
  12. Vector3 lastMousePosition;
  13.  
  14. // Use this for initialization
  15. void Start () {
  16. lastMousePosition = Input.mousePosition;
  17. }
  18.  
  19. // Update is called once per frame
  20. void Update()
  21. {
  22. //Test on Editor
  23. EditorInput();
  24.  
  25. //For touch enabled devices
  26. //TouchInput();
  27.  
  28. }
  29. void HandleTouch(Touch tch){
  30. scrubVal += tch.deltaPosition.x/Screen.width;
  31. scrubVal = Mathf.Clamp(scrubVal,-1,1);
  32. anim["CameraPath"].speed = scrubVal;
  33. }
  34. void NoTouch(){
  35. if (scrubVal < 0) {
  36. scrubVal += Time.deltaTime/inertia;
  37. }else if (scrubVal > 0) {
  38. scrubVal -= Time.deltaTime/inertia;
  39. }
  40. anim["CameraPath"].speed = scrubVal;
  41. }
  42.  
  43. void TouchInput(){
  44. if (Input.touchCount > 0) {
  45. if (Input.GetTouch (0).phase == TouchPhase.Moved) {
  46. HandleTouch(Input.GetTouch(0));
  47. }else if (Input.GetTouch (0).phase == TouchPhase.Ended) {
  48. //Slowly stops animation when touch is ended
  49. NoTouch();
  50. }
  51. }
  52. }
  53.  
  54. /// <summary>
  55. /// Following portion not needed for touch devices
  56. /// Couldn't test in any mobile devices at the moment so worked around with the following one
  57. /// </summary>
  58. void EditorInput(){
  59. if (Input.GetMouseButton (0)) {
  60. if (SimTouchInEditor().phase == TouchPhase.Moved) {
  61. HandleTouch (SimTouchInEditor());
  62. }
  63. } else {
  64. NoTouch ();
  65. }
  66. if(Input.GetMouseButtonUp(0)) lastMousePosition = Input.mousePosition;
  67. }
  68. Touch SimTouchInEditor(){
  69. Touch fakeTouch = new Touch ();
  70. fakeTouch.fingerId = 10;
  71. fakeTouch.position = Input.mousePosition;
  72. fakeTouch.deltaTime = Time.deltaTime;
  73. fakeTouch.deltaPosition = Input.mousePosition - lastMousePosition;
  74. fakeTouch.phase = (Input.GetMouseButtonDown (0) ? TouchPhase.Began :
  75. (fakeTouch.deltaPosition.sqrMagnitude > 1f ? TouchPhase.Moved : TouchPhase.Stationary));
  76. fakeTouch.tapCount = 1;
  77. return fakeTouch;
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement