Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Scrub : MonoBehaviour {
- public Animation anim;
- [Range(0.1f,10f)]
- public float inertia = 1.0f;
- float maxScrubVal = 1;
- float scrubVal = 0;
- Vector3 lastMousePosition;
- // Use this for initialization
- void Start () {
- lastMousePosition = Input.mousePosition;
- }
- // Update is called once per frame
- void Update()
- {
- //Test on Editor
- EditorInput();
- //For touch enabled devices
- //TouchInput();
- }
- void HandleTouch(Touch tch){
- scrubVal += tch.deltaPosition.x/Screen.width;
- scrubVal = Mathf.Clamp(scrubVal,-1,1);
- anim["CameraPath"].speed = scrubVal;
- }
- void NoTouch(){
- if (scrubVal < 0) {
- scrubVal += Time.deltaTime/inertia;
- }else if (scrubVal > 0) {
- scrubVal -= Time.deltaTime/inertia;
- }
- anim["CameraPath"].speed = scrubVal;
- }
- void TouchInput(){
- if (Input.touchCount > 0) {
- if (Input.GetTouch (0).phase == TouchPhase.Moved) {
- HandleTouch(Input.GetTouch(0));
- }else if (Input.GetTouch (0).phase == TouchPhase.Ended) {
- //Slowly stops animation when touch is ended
- NoTouch();
- }
- }
- }
- /// <summary>
- /// Following portion not needed for touch devices
- /// Couldn't test in any mobile devices at the moment so worked around with the following one
- /// </summary>
- void EditorInput(){
- if (Input.GetMouseButton (0)) {
- if (SimTouchInEditor().phase == TouchPhase.Moved) {
- HandleTouch (SimTouchInEditor());
- }
- } else {
- NoTouch ();
- }
- if(Input.GetMouseButtonUp(0)) lastMousePosition = Input.mousePosition;
- }
- Touch SimTouchInEditor(){
- Touch fakeTouch = new Touch ();
- fakeTouch.fingerId = 10;
- fakeTouch.position = Input.mousePosition;
- fakeTouch.deltaTime = Time.deltaTime;
- fakeTouch.deltaPosition = Input.mousePosition - lastMousePosition;
- fakeTouch.phase = (Input.GetMouseButtonDown (0) ? TouchPhase.Began :
- (fakeTouch.deltaPosition.sqrMagnitude > 1f ? TouchPhase.Moved : TouchPhase.Stationary));
- fakeTouch.tapCount = 1;
- return fakeTouch;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement