- Home /
Pan camera 2d by touch
I am wanting to pan an orthographic camera on a touch screen device by swiping. (i.e. Angry Birds) I have searched all over for a reference or example of this. I don't think I am searching for the right things because I cannot find what I'm looking for.
Can anyone give me an example or a link to an example on how to pan an orthographic camera on touch (swipe) swiping left moves camera right, swiping right moves camera left. Any info would be greatly appreciated. Thank you for your time, and consideration.
Answer by JoaquinRD · Aug 17, 2013 at 05:09 PM
Found an example in the docs here:
http://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html
Just attach a script like this to your camera and it should work (assuming your camera is oriented to point parallel to the z-axis).
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
public float speed = 0.1F;
void Update() {
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
}
}
}
Was in the only place I didn't look. I feel like such a dunce. Thank you very kindly for your time, and such an excellent answer.
Answer by alberto-lara · Oct 28, 2013 at 06:31 PM
Thanks, this works for me too, however, it's too fast and I change the line:
transform.Translate(-touchDeltaPosition.x * speed, -touchDeltaPosition.y * speed, 0);
by
transform.Translate(-touchDeltaPosition.x * speed * Time.deltaTime, -touchDeltaPosition.y * speed * Time.deltaTime, 0);
and it goes very well :)
Answer by savlon · Feb 21, 2015 at 03:13 PM
If you want pixel perfect touch control (object you place your finger over and move stays under your finger), then I suggest you check out this tutorial. It implements panning and zooming similar to clash of clans style.
The channel associated with the youtube link you provided is no longer available. Is there anyway you could share the script or point to a similar source where it $$anonymous$$ches how its implemented? thanks
Please sir can we get the tutorials from somewhere else? , We are really in need of it.
Here's some simple code I wrote to do what this answer is suggesting:
using UnityEngine;
public class CameraDrag : $$anonymous$$onoBehaviour {
private Vector3 dragOrigin; //Where are we moving?
private Vector3 clickOrigin = Vector3.zero; //Where are we starting?
private Vector3 basePos = Vector3.zero; //Where should the camera be initially?
void Update() {
if (Input.Get$$anonymous$$ouseButton(0)) {
if (clickOrigin == Vector3.zero) {
clickOrigin = Input.mousePosition;
basePos = transform.position;
}
dragOrigin = Input.mousePosition;
}
if (!Input.Get$$anonymous$$ouseButton(0)) {
clickOrigin = Vector3.zero;
return;
}
transform.position = new Vector3(basePos.x + ((clickOrigin.x - dragOrigin.x) * .01f), basePos.y + ((clickOrigin.y - dragOrigin.y) * .01f), -10);
}
}