纳金网
标题:
滑动列表时实现平滑阻尼效果修改
[打印本页]
作者:
狂风大尉
时间:
2014-3-12 21:54
标题:
滑动列表时实现平滑阻尼效果修改
初始篇:
public GameObject groupObj;
float smoothTime = 0.1F;
float yVelocity = 0.0F;
float newPosition;
float temp;
float target;
void Update () {
if(Input.touchCount>0&&Input.GetTouch(0).phase==TouchPhase.Began)
temp=groupObj.transform.position.y;
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved){
Ray rayM = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hitM;
if(Physics.Raycast (rayM, out hitM, 1000)&&hitM.transform.name=="Group"){
target=temp+Input.GetTouch(0).deltaPosition.y*10;
newPosition = Mathf.SmoothDamp(groupObj.transform.position.y,target, ref yVelocity, smoothTime);
groupObj.transform.position=new Vector3(groupObj.transform.position.x,newPosition,groupObj.transform.position.z);
}
}
else{
if(target>700)
target=700;
if(target<0)
target=0;
if(groupObj.transform.position!=new Vector3(groupObj.transform.position.x,target,groupObj.transform.position.z)){
print("move"+ target);
newPosition = Mathf.SmoothDamp(groupObj.transform.position.y,target, ref yVelocity, smoothTime);
groupObj.transform.position=new Vector3(groupObj.transform.position.x,newPosition,groupObj.transform.position.z);
}
}
}
之前的方法实现出来的效果不太好,会出现不在边界也会有,一拖动就返回的现象,所以又重新修改了一下,不同的地方在于将边界检测与正常情况拖动进行了分类处理。
正常情况的检测是通过间接控制速度,边界检测则是控制位移;之前的是列表的,只能纵向拖动,现在修改了之后改为不限定方向,只要不出一个矩形框的范围就行。
[csharp] view plaincopy
float touchDeltaPositionX;
float touchDeltaPositionY;
float maxX=0f;
float maxY=0f;
float targetX=0f;
float targetY=0f;
float newPositionX;
float newPositionY;
[csharp] view plaincopy
if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved){
touchDeltaPositionX=Input.GetTouch(0).deltaPosition.x;
touchDeltaPositionY= Input.GetTouch(0).deltaPosition.y;
maxX=Mathf.Max(maxX,Mathf.Abs(touchDeltaPositionX));
maxY=Mathf.Max(maxY,Mathf.Abs(touchDeltaPositionY));
if(touchDeltaPositionX<0)
maxX=-maxX;
if(touchDeltaPositionY<0)
maxY=-maxY;
groupObj.transform.Translate(maxX*Time.deltaTime*10,maxY*Time.deltaTime*10,0);
}
else{
if(groupObj.transform.position.y>=-170&&groupObj.transform.position.y<=670f&&groupObj.transform.position.x>=-680&&groupObj.transform.position.x<=500){
if(!Mathf.Approximately(maxX,0f)||!Mathf.Approximately(maxY,0f)){
maxX=Mathf.MoveTowards(maxX,0f,0.6f);
maxY=Mathf.MoveTowards(maxY,0f,0.6f);
}
groupObj.transform.Translate(maxX*Time.deltaTime*10,maxY*Time.deltaTime*10,0);
}
else{
maxY=0;
maxX=0;
targetX=groupObj.transform.position.x;
targetY=groupObj.transform.position.y;
if(groupObj.transform.position.x>500)
targetX=500;
if(groupObj.transform.position.x<-680)
targetX=-680;
if(groupObj.transform.position.y>670)
targetY=670;
if(groupObj.transform.position.y<-170)
targetY=-170;
newPositionX = Mathf.SmoothDamp(groupObj.transform.position.x, targetX,ref xVelocity, smoothTime);
newPositionY = Mathf.SmoothDamp(groupObj.transform.position.y,targetY, ref yVelocity, smoothTime);
groupObj.transform.position=new Vector3(newPositionX,newPositionY,groupObj.transform.position.z);
}
}
}
作者:
Chubbaz
时间:
2014-3-12 22:00
Thanks for sharing.
作者:
Lau_Chingliang
时间:
2014-3-19 15:22
谢谢分享
作者:
Mr_X
时间:
2014-3-23 09:51
直接贴代码的NB
作者:
jedi
时间:
2014-3-31 15:30
好文章 谢谢分享
欢迎光临 纳金网 (http://old.narkii.com/club/)
Powered by Discuz! X2.5