- Home /
Question by
unity_qc3pif-uPrHocQ · Jun 13, 2020 at 05:25 PM ·
rotationmovementmobiletouchmobile devices
How to rotate an object around a fixed point
Hello. 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);
}
Thank you for your help!
untitled.png
(11.7 kB)
Comment