With this code I can move the camera in my Android game with one finger.
function Update ()
{
if (Input.touchCount == 1 Input.GetTouch(0).phase == TouchPhase.Moved)
{
if (Input.GetTouch(0).deltaPosition.x < 0)
{
this.transform.position.x -= -Input.GetTouch(0).deltaPosition.x * dragSpeed;
}
if (Input.GetTouch(0).deltaPosition.x > 0)
{
this.transform.position.x -= -Input.GetTouch(0).deltaPosition.x * dragSpeed;
}
}
}
When I release my finger, it stops moving immediately.
But what I would like to have is that the camera is slowing down after releasing my finger. How can I do that?
Could always have an else statement that says if there is no touchCount, check the speed is not 0, then gradually reduces the speed (possibly with a lerp).
A simple example that show you how to make your movement have a smooth inertia.
Best effect is set moveInertia to 55.
But this algorithm have a mistake that if moveInertia*Time.deltaTime > 1,then your camera will never stop.
Solve this is easy,just lock inertia in -1 ~ 1.
But if you do this ,inertia will never greater than some any value.
Hi,
You let me know that.
Just make “curInertia” to an float,and -= Time.deltaTime*moveInertia every game loop.
And move by *curInertia.
That’s great!!
Thanks you
Awful but could be altered to be nice? (it will probably need alot of tweaking as i just quickly wrote this on my phone… Like making the speed dynamic each update instead of at load etc)
var countDown : float;
var count : float;
var dir : String;
function Start ()
{
count = dragSpeed;
countDown = dragSpeed / 4;
dir = "n";
{
function Update ()
{
if (Input.touchCount == 1 Input.GetTouch(0).phase == TouchPhase.Moved)
{
count = dragSpeed;
if (Input.GetTouch(0).deltaPosition.x < 0)
{
this.transform.position.x += Input.GetTouch(0).deltaPosition.x * count;
dir = "r";
}
if (Input.GetTouch(0).deltaPosition.x > 0)
{
this.transform.position.x -= Input.GetTouch(0).deltaPosition.x * count;
dir = "l";
}
}
else if (Input.touchCount == 0)
{
if (count > 0)
{
if (dir == "r")
{
this.transform.position.x += Input.GetTouch(0).deltaPosition.x * count * Time.deltaTime;
}
if (dir == "l")
{
this.transform.position.x -= Input.GetTouch(0).deltaPosition.x * count * Time.deltaTime;
}
count -= countDown * Time.deltaTime;
}
else
{
count = dragSpeed;
dir = "n";
}
}
else
{
dir = "n";
}
}