- Home /
 
               Question by 
               siddharth3322 · Aug 03, 2021 at 09:51 AM · 
                rotationphysics2dtouch controlsboxcollider2ddragging  
              
 
              Rotate Bar Based on Touch Drag
I want to rotate the bar based on my finger touch draw related to the pivot point. Following test structure implementation, I have created for testing purposes.

Currently, I can able to write this code:
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TestRotateController : MonoBehaviour
 {
 
     float rotateSpeed = 10f;
     Vector2 touchStartPos;
     Transform touchItem;
     //
     [SerializeField] LayerMask touchItemsMask;
 
     private void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
 
             RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero, 0f, touchItemsMask);
 
             if (hit.collider != null && hit.transform.CompareTag(GameConstants.TAG_RELEASE_ANGLE_BAR))
             {
                 touchItem = hit.transform;
                 touchStartPos = mousePos2D;
             }
         }
         else if (Input.GetMouseButton(0))
         {
             if (touchItem != null && touchItem.CompareTag(GameConstants.TAG_RELEASE_ANGLE_BAR))
             {
 
                 Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                 Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
 
                 RotateReleaseAngleBar(touchStartPos, mousePos2D);
 
             }
         }
         else if (Input.GetMouseButtonUp(0))
         {
             touchItem = null;
         }
     }
 
 
     // rotate pivot parent
     public void RotateReleaseAngleBar(Vector2 touchStartPosition, Vector2 touchPosition)
     {
         if (touchStartPosition.x > touchPosition.x)
         {
             transform.parent.Rotate(Vector3.forward, rotateSpeed * Time.deltaTime);
         }
         else if (touchStartPosition.x < touchPosition.x)
         {
             transform.parent.Rotate(Vector3.forward, -rotateSpeed * Time.deltaTime);
         }
     }
 }
Now with this code, I can't able to rotate the bar as my finger is moving. 
 Selected direction will remain proper because I have used X value to decide this but when I stop dragging my finger then also rotation remain continue in the same direction. 
 I want to stop this, I want to rotate the bar based on finger drag amount. It is a kind of experience, I want a person is rotating the bar with his finger.
 
                 
                test-rotate-bar.png 
                (104.2 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                