Trying to rotate an object around a fixed point

Viewed 82

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

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. I.e. 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. I'm not sure this is the actual problem you are having, this is more a general recommendation.

Related