Hello !
I’m doing rotation and translation using fingers.
The drag and rotation work good separately but if i rotate my object using my fingers (which works) and then try to drag my object, i’ve got a werid translation like the object goes to the opposite direction …
Here is the code of my drag :
private void HandleDrag()
{
for (var i = 0; i < Input.touchCount; i++)
{
var wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
var touchPos = new Vector2(wp.x, wp.y);
if (Input.GetTouch(i).phase == TouchPhase.Moved)
{
if (collider2D == Physics2D.OverlapPoint(touchPos))
{
var touchDeltaPosition = Input.GetTouch(i).deltaPosition;
transform.Translate(touchDeltaPosition.x * 0.01f, touchDeltaPosition.y * 0.01f, 0);
}
}
}
}
Here is the code of my rotation:
private void HandleRotation()
{
var f1 = new Touch();
var f2 = new Touch();
this._fingerIndex = 0;
for (var i = 0; i < Input.touchCount; i++)
{
var wp = Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position);
var touchPos = new Vector2(wp.x, wp.y);
if (collider2D == Physics2D.OverlapPoint(touchPos))
{
if (this._fingerIndex == 0)
{
this._fingerIndex += 1;
f1 = Input.GetTouch(i);
}
else
{
f2 = Input.GetTouch(i);
}
}
}
if (f1.phase == TouchPhase.Moved || f2.phase == TouchPhase.Moved)
{
var F1 = f1.position - f1.deltaPosition;
var F2 = f2.position - f2.deltaPosition;
var F = F2 - F1;
var f = f2.position - f1.position;
var Fatan = Mathf.Atan2(F.y, F.x);
var fatan = Mathf.Atan2(f.y, f.x);
var turnAngle = (Fatan - fatan) * (180 / Mathf.PI) * 2f;
Debug.WriteLine(turnAngle);
transform.Rotate(0, 0, -turnAngle);
}
}
Thanks for reading and helping.
Bottus.