Pinch to zoom camera: trembling when at minimum zoom
The script works perfectly, exactly as it should, except when zooming too much and the SphereCast collides with the mesh. When it hits a mesh, I want the camera to just stop, and not try to go through the mesh.
if(Input.touchCount == 2){
Touch first = Input.GetTouch(0);
Touch second = Input.GetTouch(1);
origin = mainCamera.transform.position;
direction = mainCamera.transform.forward;
RaycastHit hit;
if(Physics.SphereCast(origin, sphereRadius, direction, out hit, maxDistance)){
inCollision = true;
}
else{
inCollision = false;
}
if(first.phase == TouchPhase.Began || second.phase == TouchPhase.Began){
initialDistance = Vector3.Distance(first.position, second.position);
}
if(first.phase == TouchPhase.Moved || second.phase == TouchPhase.Moved){
movedDistance = Vector3.Distance(first.position, second.position) - initialDistance;
if(inCollision){
movedDistance = -Mathf.Abs(movedDistance);
}
mainCamera.transform.localPosition = new Vector3(0, 0, mainCamera.transform.localPosition.z + movedDistance * zoomSensitivity * Time.deltaTime);
initialDistance = Vector3.Distance(first.position, second.position);
}
}
I tried to: Set movedDistance to 0 when SphereCast hits the mesh. Clamp mainCamera.transform.localPosition.z to mainCamera.transform.localPosition.z, but only when SphereCast hits the mesh, so the z doesn't zoom more than it's current value.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612220654im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player i spining when moving 0 Answers
Multitouch on Android: position incorrect 0 Answers
How to play a 360 degree video with stereo mode in Unity 3D 0 Answers
Cannot get firebase working on android 0 Answers