- Home /
Relative rotation with touch sometimes jumps to new rotation
Hello,I have circle in my game that i use to rotate while draging my finger on the screen.When i take finger off the screen,and then drag from another place,the circle should continue rotating from previous rotation instead of instantly changing rotation.It kinda works but my problem is that sometimes - I can't tell when that happens exactly,the circle jumps to new rotation instead of continuation.Can someone tell me where i have error in my script?
Here's the exact part of the script :
if (Input.touchCount > 0)
{
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(pos.x, pos.y);
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
startAngle = Mathf.Atan2(touchPos.y,touchPos.x) * Mathf.Rad2Deg;
if (startAngle <= 0)
{
startAngle += 360;
}
}
angle = Mathf.Atan2(touchPos.y, touchPos.x) * Mathf.Rad2Deg;
if(angle < 0)
angle += 360;
distanceAngle = startAngle - endAngle;
angle -= distanceAngle;
if (angle < 0)
angle += 360;
else if (angle > 360)
angle -= 360;
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
endAngle = angle;
}
circle.rotation = Quaternion.Euler(0f, 0f,angle);
I also kinda bumped into a similar situation. Did not go through your code but try adding this before checking touch ie before line 1 :
if (Input.Get$$anonymous$$ouseButtonDown (0)) return;
You simply ignore the first touch which rotates it to a weird angle.
I'm afraid this doesn't work,it makes the rotation jump all the time :(
Answer by Eno-Khaon · Mar 17, 2016 at 12:04 AM
Let's break this down quickly:
Your object current has no rotation, so zero degrees.
You press your finger down at 45 degrees and circle around to 90 degrees. Your starting rotation is now 45 and your ending rotation is now 90.
You press your finger down at 180 and rotate around to 270. Your new starting angle is 180 and your ending angle changes from 45 to 135.
Going back to the start, that means you cancel out the difference between your finger's angle and your starting angle 45 - 45 = 0
. Then, when you release your finger, the final rotation is 45 degrees, based around endAngle = angle - startAngle
.
For the second set, you're now updating the starting angle. 180 - 180 = 0
The object is currently at 45 degrees, therefore your rotational sum comes from current - start + end
or, in this case, 180 - 180 + 45
. Rotate it around and you now have 270 - 180 + 45
for a 135 degree rotation.
With all the logic in mind, you almost had it right. A single oversight resulted in the following line:
endAngle = angle;
Your ending angle is not accommodating any starting angle aside from 0 degrees. Instead, it should read:
endAngle = angle - startAngle + endAngle;
If you are saying that i have to change endAngle = angle
to endAngle = angle - startAngle
in my "Ended" phase,this makes the rotation even more messy,it never start rotation from the good angle.I think my endAngle part is good,because it just remembers the last angle i had my finger on.Or just i can't exactly understand what do you mean.Can you please show the whole code with $$anonymous$$e things changed that you say are wrong?
Ack! I only just found this reply -- Sorry for my late response.
A general overview of the code would look something like this:
// On initial touch
startingAngle = $$anonymous$$athf.Atan2(touchPos.y,touchPos.x);
// During active touch and motion
touchAngle = $$anonymous$$athf.Atan2(touchPos.y, touchPos.x);
float difference = (touchAngle - startingAngle) * $$anonymous$$athf.Rad2Deg;
float currentTotal = (touchAngle - startingAngle + endingAngle) * $$anonymous$$athf.Rad2Deg;
// Touch ends, finger lifted
endingAngle = touchAngle - startingAngle + endingAngle;
float finalTotal = endingAngle * $$anonymous$$athf.Rad2Deg;
Realizing now that I also forgot to add endAngle to its own value, I've edited the main reply to match.
Your answer
Follow this Question
Related Questions
Make a navmesh object start moving in a circle around an object 0 Answers
Transform.eulerAngles only x and z 2 Answers
Rotation around z-axis always local space and not world? Vector-axis angle projection calcuations. 0 Answers
Creating circles using script 1 Answer
2D physics object acts weird when pressed into other physics object 0 Answers