- Home /
Fix Rotate Object with touchscreen on X and Y axis
guys, i need a little bit help, already done googling but still not found any solution so i want to rotate my object with a touch input, heres the code
target.transform.Rotate(touchDeltaPosition.x 3.0f,touchDeltaPosition.y 3.0f ,0,Space.World);
it works perfectly, but the problem is it rotate diagonally, i need it to fix rotate only on x and y axis, i already done quite lot of validation and solution i found on this forum and stackoverflow, but still nothing
heres a simple drawn i made to made it a little bit more clear http://i.imgur.com/cAeg8yr.png what i need is the object to rotate fix on X and Y axis (the green line) , but unfortunately with my script, it'll also rotate horizontally (the red line) and i don't want it
i'm using unity 5
thank you in advance
If you mean you don't want to rotate in Z, you could add another line of code after your touch code that limits the z-axis to 0.
target.transform.rotation = Quaternion.Euler(target.transform.rotation.EulerAngles.x, target.transform.rotation.eulerAngles.y, 0);
But if you are rotating freely on the X and Y axis, then there's going to be diagonal movement, unless you limit it to one axis at a time. (If you rotate something left and up at the same time, then it is rotating diagonally, correct?).
indeed, i already try write validation for that, but unfortunately it still not working correctly, could you see what is going wrong?
if(!vertical && !horizontal)
{
float swipeDistVertical = (new Vector3(0, touchDeltaPosition.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistVertical > 0)
{
float swipeValue = $$anonymous$$athf.Sign(touchDeltaPosition.y - startPos.y);
if (swipeValue > 0 || swipeValue < 0)//up swipe
{
vertical = true;
horizontal = false;
}
}
float swipeDistHorizontal = (new Vector3(touchDeltaPosition.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > 0)
{
float swipeValue = $$anonymous$$athf.Sign(touchDeltaPosition.x - startPos.x);
if (swipeValue > 0 || swipeValue < 0)//right swipe
{
horizontal = true;
vertical = false;
}
}
}
if(vertical)
{
target.transform.Rotate(touchDeltaPosition.y * 3.0f, 0,0,Space.World);
}
if(horizontal)
{
target.transform.Rotate(0,touchDeltaPosition.x * 3.0f,0,Space.World);
}
}
The vertical and horizontal booleans are set to false when the touch begins.
Just to be clear, are you trying to limit the rotation to either y-axis or x-axis depending on whether you swipe up/own or left/right?
yeah, not exactly swipe tho, but drag the touch input, that's why i use delta position ins$$anonymous$$d touch ended phase
Your answer
Follow this Question
Related Questions
Touch Input positions are different on iOS and Android? 2 Answers
Raycast from player to touch point? 2 Answers
Android double jump not working correctly 1 Answer
Avoid touch input when pressing the "Pause" button 1 Answer
Mobile FPC in unity 5 1 Answer