Unity 慣性滑動(思想可以用於類似攝像機,Scroll)

using UnityEngine;
using System.Collections;

 public class Drag : MonoBehaviour
 {
 private Vector3 _screenPoint;
 private Vector3 _offset;
 private Vector3 _curScreenPoint;
 private Vector3 _curPosition;
 private Vector3 _velocity;
 private bool _underInertia;
 private float _time = 0.0f;
 public float SmoothTime;

 void Update()
 {
     if(_underInertia && _time <= SmoothTime)
     {
         transform.position += _velocity;
         _velocity = Vector3.Lerp(_velocity, Vector3.zero, _time);
         _time += Time.smoothDeltaTime;
     }
     else
     {
         _underInertia = false;
         _time = 0.0f;
     }
 }
 void OnMouseDown()
 {
     _screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     _offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, _screenPoint.z));
     //Screen.showCursor = false;
     _underInertia = false;
 }
 void OnMouseDrag()
 {
     Vector3 _prevPosition = _curPosition;
     _curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, _screenPoint.z);
     _curPosition = Camera.main.ScreenToWorldPoint(_curScreenPoint) + _offset;
     _velocity = _curPosition - _prevPosition;
     transform.position = _curPosition;
 }
 void OnMouseUp()
 {
     _underInertia = true;
     //Screen.showCursor = true;
 }
 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章