Unity 滑动列表时实现平滑阻尼效果初始篇

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






你可能感兴趣的:(unity)