Search Unity

Reading and using Touch acceleration in moving camera?

Discussion in 'Scripting' started by Zythus, Apr 14, 2017.

  1. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    30
    Hello, I've been trying to achieve moving camera by using acceleration of the touch; i.e. to move camera slow or fast relatively to how quickly the player moved his finger on the screen. So far I've only got moving camera along X axis to the right and to the left. But the speed at which the camera moves is constant.

    I tried to use Input.GetTouch(0).deltaPosition.magnitude and dividing by Input.GetTouch(0).deltaTime but didn't have any success with these.

    Code (CSharp):
    1.   touchPointX = Input.GetTouch(0).deltaPosition.x;
    2.   float newPosition = Mathf.SmoothDamp(0.1f, -5.5f, ref velocityF, 0.3f); // also tried to achieve smoothness of the camera after releasing the finger
    3.   transform.RotateAround(player.transform.position, (new Vector3(-0.0f, touchPointX, 0.0f)), newPosition * 10);
     
  2. Zythus

    Zythus

    Joined:
    Jun 28, 2014
    Posts:
    30
    Okay, after a couple days of looking through stuff and seeing multiple advices with ScreenToWorldPoint I decided to give it a go. And thus achieved the acceleration when moving camera with touch. Now all that is left is to make it smooth after releasing the finger, instead of the move being finished suddenly.
    Here's the code that I have right now if anyone ever comes across this problem themselves.

    Code (CSharp):
    1. Vector2 TouchDirection = Input.GetTouch(0).deltaPosition;
    2. Vector3 worldVector = Camera.main.ScreenToWorldPoint(new Vector3(0, TouchDirection.x, 0));
    3. transform.RotateAround(player.transform.position, (new Vector3(0.0f, worldVector.y, 0.0f)), TouchDirection.x * 0.37f);