- //当进入决斗模式时,判断玩家是否移动摇杆
- if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved){
- Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
- RaycastHit hit;
- if(Physics.Raycast (ray, out hit, 1000)){
- print(hit.transform.name.ToString());
- if(hit.transform.name=="rockerColorUp"){
- //获取手势移动的速度
- Vector2 touchDeltaPosition= Input.GetTouch(0).deltaPosition;
- float distance=Mathf.Sqrt(Mathf.Pow(touchDeltaPosition.x,2f)+Mathf.Pow(touchDeltaPosition.y,2f));
- velocity=Mathf.Abs(distance/Time.deltaTime);
- velocity=Mathf.Sqrt(velocity)/10000;
- //根据手势移动速度决定能量条的增长,同时能量条有一个固定的减速
- GameSetting.energyBarValue+=(velocity-GameSetting.BarSpeedDown[GameSetting.CompetitorNow.id-1]);
- GameSetting.energyBarValue=1;
- energyBar.UpdateBar_MJM(GameSetting.energyBarValue,false)
- //控制摇杆的移动
- if(target){
- rocker.transform.Rotate(0,0,-500*Time.deltaTime);
- }
- else{
- rocker.transform.Rotate(0,0,500*Time.deltaTime);
- }
- if(rocker.transform.localEulerAngles.z<=340&&rocker.transform.localEulerAngles.z>=20&&target){
- target=false;
- rocker.transform.localEulerAngles=new Vector3(0,0,340);
- }
- if(rocker.transform.localEulerAngles.z>=20&&rocker.transform.localEulerAngles.z<=340&&!target){
- target=true;
- rocker.transform.localEulerAngles=new Vector3(0,0,20);
- }
- }
- }
- }
- else{
- //在能量条值大于0时,且玩家没有手势动作时,能量条有一个固定的减速
- if(GameSetting.energyBarValue>=0){
- GameSetting.energyBarValue-=GameSetting.BarSpeedDown[GameSetting.CompetitorNow.id-1];
- energyBar.UpdateBar_MJM(GameSetting.energyBarValue,false);
- }
- rocker.transform.localEulerAngles=new Vector3(0,0,0);
- }
||||||||||||||||||||||||||| 获取手指移动的路程 以及 手指移动的速度 Unity |||||||||||||||||||||||||||
为了获得精确值,可以在FixedUpdate里面执行,同时也可以减小计算量,更改FixedUpdate的更新速率的位置为Edit->Project Setting->Time。
- float distance;
- void start(){
- distence=0;
- }
- void FixedUpdate{
- if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved){
- Vector2 touchDeltaPosition= Input.GetTouch(0).deltaPosition;
- float temp=Mathf.Sqrt(Mathf.Pow(touchDeltaPosition.x,2f)+Mathf.Pow(touchDeltaPosition.y,2f));
- // 路程
- distence+=temp;
- //速度
- velocity=Mathf.Abs(temp/Time.deltaTime);
- }
- else{
- distence=0;
- }
- }