简体   繁体   English

试图围绕固定点旋转 object

[英]Trying to rotate an object around a fixed point

I'm working on a mobile game and I want to rotate the red circle around the black circle in reference to the center of the circle by moving the finger up or down on the screen.我正在开发一款手机游戏,我想通过在屏幕上向上或向下移动手指来围绕黑色圆圈旋转红色圆圈。 I already wrote a code but I have an issue, when I change the direction of the finger's movement too fast it doesn't change the movement direction of the red circle.(The blue circle represent the finger and those 2 arrows its direction of movement).我已经写了一个代码,但是我有一个问题,当我改变手指运动的方向太快时,它不会改变红色圆圈的运动方向。(蓝色圆圈代表手指,那两个箭头代表它的运动方向)。 Hopefully you understood what I'm trying to say.希望你明白我想说什么。

Here's my code:这是我的代码:

 private Vector3 fp;   //First touch position
 private Vector3 lp;   //Last touch position
 private float dragDistance;  //minimum distance for a swipe to be registered
 public static float movement = 0f; //direction

 void Start()
 {
     movement = 0f;
     dragDistance = Screen.height * 1 / 100; //dragDistance is 1% height of the screen
 }
 void Update()
 {
     if (Input.touchCount == 1) 
     {
         Touch touch = Input.GetTouch(0); 
         if (touch.phase == TouchPhase.Began) 
         {
             fp = touch.position;
             lp = touch.position;
         }
         else if (touch.phase == TouchPhase.Moved)
         {
             moveSpeed = Input.touches[0].deltaPosition.magnitude / Input.touches[0].deltaTime;

             lp = touch.position;
             if (Mathf.Abs(lp.y - fp.y) > dragDistance)
             {      

                     if (lp.y > fp.y) 
                     {   //Up swipe
                         movement = -1f;
                     }
                     else
                     {   //Down swipe
                         movement = 1f;
                     }            
             }
             if(lp.y > 0)
             {
                 if (Input.touches[0].deltaPosition.magnitude < lp.y)
                     movement *= -1f;
             }
             if (lp.y < 0)
                 if (Input.touches[0].deltaPosition.magnitude > lp.y)
                     movement *= -1f;

         }
         else if(touch.phase == TouchPhase.Stationary)
         {
             movement = 0f;
             fp = touch.position;
             lp = touch.position;
         }
         else if (touch.phase == TouchPhase.Ended) 
         {
             lp = touch.position;  
             movement = 0f;
         }
     }
 }
 private void FixedUpdate()
 {
      transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
 }

If I understand this correctly, the finger controls rotation directly, and not rotation-speed?如果我理解正确,手指直接控制旋转,而不是旋转速度?

If so I would suggest saving the initial rotation, and simply use the difference in y value from the initial touch position to the current touch position to produce a new rotation.如果是这样,我建议保存初始旋转,并简单地使用从初始触摸 position 到当前触摸 position 的 y 值差异来产生新的旋转。 Ie create a copy of the original transform and rotate this, overwriting the transform from the previous frame.即创建原始变换的副本并旋转它,覆盖前一帧的变换。

I have had problems with applying delta rotations since small errors will accumulate.我在应用增量旋转时遇到了问题,因为会累积小错误。 So if you start dragging your finger wildly, the rotation might start behaving odd, and not return to neutral when returning the finger to the original position.因此,如果您开始疯狂地拖动手指,旋转可能会开始表现得很奇怪,并且在将手指返回到原始 position 时不会返回中性。 I'm not sure this is the actual problem you are having, this is more a general recommendation.我不确定这是您遇到的实际问题,这是更一般的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM