The code below works fine – when player moves finger in the top 80% of the screen, the camera goes up/down and all that.
however, as soon as they let go, it stops. I’d like them to be able to do a swipe that slowly stops, rather than right away. Any ideas on how to achieve that?
Thanks!
for (var i = 0; i < Input.touchCount; ++i)
{
if (Input.GetTouch(i).position.y > (screenHeight / 10) * 2) // if the touch is in the top 80% of the screen (the play field, basically)
{
if (Input.GetTouch(i).phase == TouchPhase.Moved)
{
// Finger moved, so it's a swipe
var touchDeltaPosition:Vector2 = Input.GetTouch(i).deltaPosition;
playerObject.transform.eulerAngles.x += (-touchDeltaPosition.y * rotateSpeed);
playerObject.transform.eulerAngles.y -= (-touchDeltaPosition.x * rotateSpeed);
}
}
}
There are various ways to achieve what I believe you want. Usually, these types of problems are better solved if you try to use helping variables. For example, two possibilities I can hint you:
Store the last position of the touch that moves the camera. Then, you gradually move that cameras with a function that depends on the distance. If you’re not really into maths, try moving, for example, (distance from camera to position) * percentagge * Time.deltaTime every frame. If you’re into maths, you can try using the Vector3.Lerp with the positions. Lerp uses a parameter ranging from 0 to 1 to decide the resulting value, so you can use any trick as (elapsed time / total animation time) ^ x (x from 1 to any number), or achieving really smooth effects by using Mathf.Sin((elapsed time / total time) * Mathf.PI). Actually, any continuous function where f(0) = 0 and f(1) = 1 with f’(0) = f’(1) = 0 will have a good smooth effect. Talk about calculus =D. Total time is the time you want the effect to take to execute, and elapsed time is the time its been executing. I suggest using times with lerp because you can tune how long the effect will take.
Use the swype to calculate an acceleration, and then move the camera by using that acceleration. You can make a big deceleration if the touch ends.
I guess I did a pretty confuse attempt of an answer…