- Home /
Weird translatation after rotation
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.
Comment
Answer by bottus · Nov 05, 2014 at 09:35 AM
I finally managed to solve the problem.
Here is the solution :
var tmpRotation = transform.rotation.eulerAngles;
transform.rotation = Quaternion.Euler(this._initialRotation);
transform.Translate(touchDeltaPosition.x * 0.01f, touchDeltaPosition.y * 0.01f, 0);
transform.rotation = Quaternion.Euler(tmpRotation);
i just needed to reset the transform.rotation to its initial state before translating and then set it back to its real value.
Your answer
![](https://koobas.hobune.stream/wayback/20220613170716im_/https://answers.unity.com/themes/thub/images/avi.jpg)