Unity touch zoom camera

0

I'm working with this code below to control the distance from the camera to the character, this distance is the result of linear interpolation between a minimum and maximum offset, where the variable float distance controls this result.

Vector3.Lerp(minOffset, maxOffset, distance)

But in the zoom function the result of the Vector2.Distance is being quite large, between 110,000 and 890,000, which is certainly far from the minimum and maximum required to control Lerp.

void Zoom()
{
    if (Input.touchCount != 2)
        return;

    Touch touch0 = Input.GetTouch(0);
    Touch touch1 = Input.GetTouch(1);

    if(touch0.phase == TouchPhase.Moved || touch1.phase == TouchPhase.Moved)
    {
        distance = Vector2.Distance(touch0.position, touch1.position);
    }
}

I've tried using Mathf.Clamp , but logically the value of Vector2.Distance is too large for the result to be less than 1F.

distance = Mathf.Clamp(Vector2.Distance(touch0.position, touch1.position), 0F, 1F)

How can I reduce the minimum and maximum from Vector2.Distance to min 0F and max 1F?

Remember that I should consider that in some devices the result between touch0 and touch1 in Vector2.Distance may be greater.

    
asked by anonymous 06.08.2016 / 06:37

1 answer

0

I'm currently running Unity3D for testing, but I get the impression that Touch.position returns a Vector2 with the coordinate on the screen . Thus, the distance calculation will return a measurement in pixels, so that the higher the resolution of the displayed image, the greater the number returned to the same finger positions.

So what you can try to do is calculate the distance between the vectors of the normalized positions, using the normalized :

distance = Vector2.Distance(touch0.position.normalized, touch1.position.normalized);

A normalized vector has the coordinates adjusted so that its magnitude becomes 1. Thus, its calculation of distance between the normalized coordinates is independent of the resolution of the screen used. Note that this distance will be small and with decimal variations. Use it to consider the distance of the camera in a similar way to what you are already doing.

    
07.08.2016 / 22:08