- Home /
Rotate camera based on left/right swipe
Hi!
I'm trying to rotate a parent object as a pivot point with my camera as the child. I want to change the rotation y-value of the parent object based on the swipe distance. The problem is that the object rotates too fast and makes it look like it's jumping on the screen. Also when I swipe left or right it's not going to the right direction. I'm not sure what's wrong other then my distanceMag variable is too big. And is this the correct way to solve this problem? Any help or tips would be appreciated. Thanks!
if (Input.touchCount > 0)
{
foreach (var touch in Input.touches)
{
switch (touch.phase)
{
case TouchPhase.Began:
prevFingerPos = touch.position; break;
case TouchPhase.Moved:
if (touch.fingerId == 0)
{
currentFingerPos = touch.position;
touchDistance = Vector2.Distance(prevFingerPos, currentFingerPos);
float timePassed = Time.deltaTime;
float touchSpeed = touchDistance * timePassed;
newDistance = Vector2.Lerp(prevFingerPos, currentFingerPos, touchSpeed);
distanceMag = newDistance.magnitude;
//TestRotation(distanceMag);
Rotate(distanceMag);
}
break;
case TouchPhase.Stationary:
prevFingerPos = currentFingerPos; break;
case TouchPhase.Ended:
touchDistance = 0;
prevFingerPos = currentFingerPos = new Vector2();
break;
}
}
}
void Rotate(float distance)
{
Vector3 tempLocalRotation = transform.localRotation.eulerAngles;
float rotateY;
if (distance < 0) { rotateY = tempLocalRotation.y - distance; }
else if (distance > 0) { rotateY = tempLocalRotation.y + distance; }
else { rotateY = tempLocalRotation.y; }
float min = -360, max = 360;
rotateY = Mathf.Clamp(rotateY, min, max);
rotateAroundYAxis = new Vector3(0, rotateY, 0);
transform.localEulerAngles = rotateAroundYAxis;
}
Answer by Harinezumi · Apr 09, 2018 at 12:19 PM
I haven't used touch controls before, but I think you should update prevFingerPos
at the end of TouchPhase.Moved
as well, or the distance will become incrementally bigger, causing non-linear rotation in relation to the distance moved.
Also, I'm not sure what you want to express with the part where you calculate touchSpeed
(what is distance multiplied by time?) and newDistance
... you could just use touchDistance.magnitude
for the rotation.
Next, it is rotating in the wrong direction, because you subtract when distance
is negative, which is addition, counter-clockwise rotation, then add it when it is positive, which is again addition. Instead, just add it as it is, no need to check its sign.
Thanks Harizumi for the tips. I've added prevFingerPos add the end of TouchPhase.$$anonymous$$oved
as well. I wanted to know the swipe speed so ins$$anonymous$$d of touchSpeed
, I should call it swipeSpeed
. Also I've altered my code and I'm no longer using my own Rotate function anymore. Ins$$anonymous$$d I'm using the Rotate function provided by Unity and rotate to the direction based on the left and right swipe. This works but has a problem. When I stop swiping, the object keeps rotating for a bit more until it stops. And also it lags when I switch direction. Is there any way to counter this? Perhaps putting a check within the TouchPhase.$$anonymous$$oved
to check if swipe has stopped? That would be a bad solution I think..
Can you show some code? It is difficult to deter$$anonymous$$e from textual description what could be the problem.
Can't seem to add the code properly :/ switch (touch.phase) { case TouchPhase.Began: prevFingerPos = touch.position; break; case TouchPhase.$$anonymous$$oved: if (touch.fingerId == 0) { currentFingerPos = touch.position; swipeDistance = Vector2.Distance(prevFingerPos, currentFingerPos); float swipeSpeed = swipeDistance / Time.deltaTime; newDistance = Vector2.Lerp(prevFingerPos, currentFingerPos, swipeSpeed); float rotation = newDistance.magnitude Time.deltaTime inputSensitivity; //swipe right if (prevFingerPos.x < currentFingerPos.x){ transform.Rotate(Vector3.up, rotation); //swipe left }else if (prevFingerPos.x > currentFingerPos.x){ transform.Rotate(Vector3.down, rotation); } if(touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Ended) { // smooth rotation here or in the switch case } prevFingerPos = touch.position; } break; case TouchPhase.Stationary: prevFingerPos = touch.position; break; case TouchPhase.Ended: prevFingerPos = currentFingerPos = new Vector2(); break; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612160402im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
TouchPhase doesn't end when player swipes off screen 1 Answer
mouse input to touch input help please 0 Answers
Swipe menu, problem! 0 Answers
Deactivate Swipe Controls on Pause? 0 Answers