1

在我的游戏中,我希望相机根据用户滑动手指而移动。我的问题是,当有人将手指从屏幕上移开并再次放下时,相机会跳到一个新位置......我猜它与坐标有关,我这样说是因为如果我点击远离当我将手指从屏幕上移开时,相机会跳跃。这是代码:

 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.

         }
     }
 }

有什么解决方案可以让相机恢复,一旦有人再次放下手指就不会跳起来?

如果有更好的解决方案/更有效的方法来解决问题,我也愿意重做。

非常感谢!

4

1 回答 1

0

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

于 2013-10-29T14:25:19.540 回答