- Home /
Question by
king32 · Nov 08, 2020 at 03:39 PM ·
scripting problem
How to Zoom and Orbit the camera around an origin for mobile
What would be the best way to create zoom and orbit controls for a mobile touchscreen that uses simple finger sliding for orbiting around an origin in 3D and pinch gesture for zooming in and out?
I saw below posts
https://answers.unity.com/questions/1549813/camera-orbit-around-planet.html
https://forum.unity.com/threads/mobile-touch-to-orbit-pan-and-zoom-camera-without-fix-target-in-one-script.522607/
https://answers.unity.com/questions/1065722/how-to-make-camera-rotate-spherically-around-an-ob.html
I am looking for a general solution but these seem like special cases with a lot of code for very basic functionality in my opinion.
Here is the code I have started with:
public class OrbitZoomCam : MonoBehaviour
{
public Transform Origin;
public Vector3 CamAngle; // change for orbit
public Vector3 CamRadius; // change for zoom
public float dZoom; //delta zoom
public float dOrbit; //delta orbit
void Start()
{
//Translate Cam to Origin.position and add CamRadius to its z axis;
//Rotate Cam to look at Origin; store default angle in CamAngle
}
void Update()
{
//Check touch screen for pinch
handlePinch();
//Check touch screen for finger slide
handleFingerSlide();
}
private void handlePinch()
{
//get pinch in or out and add to CamRadius
//use new CamRadius and find rect coord for new position of camera
//translate cam to new position
}
private void handleFingerSlide()
{
//touchscreen is only x and y
//so how to map x and y to spherical rotation?
}
}
Comment
Your answer