With this code I can move the camera in my Android game with one finger. Code (csharp): 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?
I think I need at least the speed of the touch: Code (csharp): speedTouch = Input.GetTouch(0).deltaPosition.x; But how can I slow down this speed to zero? Something with Lerp?
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).
Hi,I have a idea that using inertia. Look at this code: Code (csharp): Vector3 curInertia = Vector3.zero; void Update(){ if(Input.GetKey(KeyCode.W)) SetMove(Vector3.forward); if(Input.GetKey(KeyCode.S)) SetMove(Vector3.back); if(Input.GetKey(KeyCode.A)) SetMove(Vector3.left); if(Input.GetKey(KeyCode.D)) SetMove(Vector3.right); } void SetMove(Vector3 dir){ curInertia = dir; } IEnumerator Move(){ thisT.hasChanged = true; while (true) { thisT.Translate (curInertia * mMoveSpeed * Time.deltaTime, Space.World); if(thisT.hasChanged){ curInertia *= moveInertia*Time.deltaTime; thisT.hasChanged = false; } yield return null; } } 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. Until now,I don't have a prefect solution And I will tell you if I got it !!
Hi Herbie, I am not shure but when you start a timer like this: Code (csharp): // set dragspeed_timer in Start function to a value you want. dragspeed_timer -= Time.deltaTime; if (dragspeed_timer < 0) DoSomething; dragSpeed = dragspeed_timer -> So the variable dragSpeed should go to cero
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) Code (csharp): 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"; } }
Code (csharp): class Movement : MonoBehaviour { float dragX = 0; float mass = 0.2f; bool dragging = false; float prevX = 0; void OnEnable() { StartCoroutine(Inertia()); StartCoroutine(Input()); } IEnumerator Inertia() { while(enabled) { if(!dragging) dragX = Mathf.moveTowards(dragX,0,mass * Time.deltaTIme); yield return null; } yield return null; } IEnumerator Input() { while(enabled) { dragX = Input.GetTouch(0).deltaPosition.x * dragSpeed; dragging = (prevX != dragX); prevX = dragX; yield return null; } yield return null; } } Untested, but a good start to what you want.