近期热门
粉丝909
关注 205
获赞 1007
Unity滑动列表时实现平滑阻尼效果修改篇

[U3D] Unity滑动列表时实现平滑阻尼效果修改篇

[复制链接]
4862 0 0 0 12年前 举报
初始篇:

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);

}

}

}



原文链接:http://blog.csdn.net/quannenggou/article/details/7305457
0
点赞
0
打赏
0
添加到收藏夹

0

点击复制链接

使用微信扫码分享
一次扣10个券
全部评论0
您需要登录后才可以回帖 登录

暂无评论,去成为第一人吧
您当前使用的浏览器IE内核版本过低会导致网站显示错误

请使用高速内核浏览器或其他浏览器