Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by MisterBiscuit · Aug 17, 2013 at 04:58 PM · touchscreenorthographicpanning

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
16
Best Answer

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);
         }
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MisterBiscuit · Aug 17, 2013 at 05:25 PM 0
Share

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.

avatar image
3

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 :)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Pan and Zoom Video Tutorial

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ams0 · Sep 19, 2016 at 04:01 PM 1
Share

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

avatar image Joy-Kumar · Jan 30, 2017 at 06:41 PM 0
Share

Please sir can we get the tutorials from somewhere else? , We are really in need of it.

avatar image Yamist · Nov 22, 2017 at 07:22 AM 0
Share

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);
     }
 }
 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pan an isometric ortho camera 2 Answers

Basic Outline (Toony) doesn't work with orthographic view? 1 Answer

2D camera zoom in comparison to the height of target 2 Answers

Orthographic Camera 1 Answer

Ortho-Isometric camera not playing nice with culling 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges