- Home /
Facing problem in the code for panning and zooming camera smoothly through touch inputs [3D]?
I am trying to move my camera smoothly over the terrain with touch inputs but not getting pure smoothness like any other high quality games And after zooming motion starts becoming slower and slower. Please help me out.
Here is my code:
public float speed = 0.1f;
public float MIN_X = 7f;
public float MAX_X = 12f;
public float MIN_Y = 8f;
public float MAX_Y = 13f;
public float MIN_Z = 10f;
public float MAX_Z = 10f;
public Vector3 nextPosition;
public float moveSpeed;
public int camAdjust = 10;
void Update () {
if (Input.touchCount == 1 && Input.GetTouch (0).phase == TouchPhase.Moved) {
Vector3 touchdeltaPosition = Input.GetTouch (0).deltaPosition;
transform.Translate (-touchdeltaPosition.x * speed*Time.deltaTime, -touchdeltaPosition.y * speed*Time.deltaTime, 0);
transform.position = new Vector3(
Mathf.Clamp(transform.position.x, MIN_X, MAX_X),
Mathf.Clamp(transform.position.y, MIN_Y, MAX_Y),
Mathf.Clamp(transform.position.z, MIN_Z, MAX_Z));
}
}
and now this script is for zooming.
public float perspectiveZoomSpeed = 0.5f;
public float orthoZoomSpeed = 0.5f;
void Update()
{
if (Input.touchCount == 2)
{
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
if (GetComponent<Camera>().orthographic)
{
GetComponent<Camera>().orthographicSize += deltaMagnitudeDiff * orthoZoomSpeed;
GetComponent<Camera>().orthographicSize = Mathf.Max(GetComponent<Camera>().orthographicSize, 0.1f);
}
else
{
GetComponent<Camera>().fieldOfView += deltaMagnitudeDiff * perspectiveZoomSpeed;
GetComponent<Camera>().fieldOfView = Mathf.Clamp(GetComponent<Camera>().fieldOfView, 20f, 80f);
}
}
}
I can't really wrap my head around your code, but here is how I do it (line 50 -56): https://github.com/ProjectS$$anonymous$$rs/TestPrototype2/blob/master/Assets/Scripts/Camera$$anonymous$$ovement.cs
Thankyou for the reply, @ASPePeX . I am facing a few errors like the Config.$$anonymous$$axCameraOffset in lines 19,20,54... etc. Apparently Config is missing. and also Helper.CalcTouchAngle from line 42. I really don't know what shall i do now.
Those classes are in the same folder, Config.cs and Helper.cs: https://github.com/ProjectS$$anonymous$$rs/TestPrototype2/tree/master/Assets/Scripts
Thankyou very much!, i will check and let you know soon.
Switch to FixedUpdate and Time.fixedDeltaTime ins$$anonymous$$d of Update, and it will be a lot smoother. The camera is your main visual output, so you are going to not a lot more jittery movement from it than you would from something else.