2013-10-29 23 views
1

在我的游戏中,我希望摄像头根据用户的手指滑动而移动。我的问题是,当有人将他们的手指从屏幕上移开并重新放下时,相机跳到一个新的位置......我猜测它与坐标有关,我这样说是因为如果我点击远处的某个地方我把手指从屏幕上移开,相机跳了起来。下面是代码:拖动基于触摸的摄像头(Unity)

var speed : int = 1; 

var lastPoint : Vector2; //make this private if you want to. 

function Update() 
{ 

    var offset : float; //offset of the touch from last frame  

    if(Input.touches.Length 0)//make sure we have a touch in the first place 
    { 

      var evt : Touch = Input.touches[0]; //setting up touch events so we can get fancy. 
      if(evt.phase == TouchPhase.Began) //this is the first frame the screen has been touched for, simply save the point. 
      { 
       lastPoint == evt.position; 
      } 
      else if(evt.phase == TouchPhase.Moved 
      { 
       offset = evt.position.x - lastPoint.x ;//take the difference 

       //I'm going to use Transform.Rotate because it's handy dandy 

       transform.Rotate(0,offset * speed,0); 

       //save the new "lastPoint" 

       lastPoint = evt.position; 
     } 
     else if(evt.phase == TouchPhase.Ended) 
     { 
      //If you want the object to drift after you spin it you can make a function to go here. 
      //To do this, take the speed of the rotation and continue to rotate all while subtracting off of the speed. 
      //I would use the Transform.Rotate function on this too. 
      //If you need me to I could write this function too. 

     } 
    } 
} 

什么解决的办法有相机简历,一旦有人再放他们的手指下降不跳?

我也愿意重做吧,如果有解决这个问题的一个更好的解决方案/更有效的方法..

非常感谢!

回答

0

提供的代码看起来很好,您只需要在用户不按下时更新每个帧的lastPoint。或者,如果您知道用户何时开始按下,只需在计算偏移量之前将lastPoint设置为当前位置。这样偏移量就是用户触摸屏幕的第一帧。