Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by J.C Burns · Feb 28, 2016 at 09:06 PM · ioscamera-movementtouch controlspanning

Camera Panning fail - erratic movement and then stops

my script is supposed to find the difference between two raycast intersections with a plane and move the camera accordingly. On the beginning of a touch a ray is cast from screenpoint and then on the move phase another ray is cast. This repeats every frame while the finger is still on-screen and moving. I essentially want to calculate the delta position of each raycast and then move the camera intuitively in a "panning motion" (my reasoning for using rays and not just the touch positions is so that i can acheive a more realistic panning effect where it would appear that the finger is actually dragging the landscape itself similar to clash of clans but with a perspective camera instead of an ortho.

public class panTest : MonoBehaviour {

 public Plane panPlane;

 Vector3 oldPoint;
 Vector3 newPoint;
 Vector3 deltaPoint;

 // Use this for initialization
 void Start () {
     panPlane.SetNormalAndPosition(Vector3.up, Vector3.up * 5);
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.touchCount == 1)
     {
         if (Input.GetTouch(0).phase == TouchPhase.Began)
         {
             Ray panRay = GetComponent<Camera>().ScreenPointToRay(Input.GetTouch(0).position);
             float hitDist;

             if (panPlane.Raycast(panRay, out hitDist))
             {
                 Debug.Log("Hit");
                 oldPoint = panRay.GetPoint(hitDist);
             }
         }
         if (Input.GetTouch(0).phase == TouchPhase.Moved)
         {
             Ray panRay = GetComponent<Camera>().ScreenPointToRay(Input.GetTouch(0).position);
             float hitDist;

             if (panPlane.Raycast(panRay, out hitDist))
             {
                 Debug.Log("Hit");
                 //gets difference in finger positions
                 newPoint = panRay.GetPoint(hitDist);
                 deltaPoint = newPoint - oldPoint;
                 oldPoint = newPoint;

                 //moves camera according to diff in finger pos'
                 transform.Translate(-deltaPoint.x, 0, -deltaPoint.z);
             }
         }
         if (Input.GetTouch(0).phase == TouchPhase.Ended)
         {

         }
     }
 }

when i run this script and touch the screen and move my finger the camera moves several million units in a direction and then fails and becomes completely unresponsive. I am very confused as to why this happens so any help would be greatly appreciated

P.S. if anyone has another way that this effect can be achieved with a perspective camera PLEASE let me know!! I am desperate for a working camera panning control (using touch) that is intuitive, smooth, and uses this "touch point- accurate panning" effect

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Political Peace Party Studios · Feb 28, 2016 at 10:41 PM

This is the code we use to make the camera pan left and right.

  using UnityEngine;
  public class ScrollCamera : MonoBehaviour
  {
  public float dragSpeed = 1;
  private Vector3 dragOrigin;
 
  void Update()
  {
  if (Input.GetMouseButtonDown(0))
  {
      dragOrigin = Input.mousePosition;
      return;
  }
  
  if (!Input.GetMouseButton(0)) return;
  Vector3 pos = Camera.main.ScreenToViewportPoint(Input.mousePosition - dragOrigin);
  Vector3 move = new Vector3(pos.x * dragSpeed, 0);
  
  transform.Translate(move, Space.World);  
  var posi = transform.position;
  posi.x =  Mathf.Clamp(transform.position.x, -1.0f, 1.96f);
  transform.position = posi;
          }
 
     }

Just stick it on your main camera and test it out.

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 J.C Burns · Feb 29, 2016 at 02:33 AM 0
Share

Thank you I will try that :)

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

59 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 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 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

Using touch (swiping) to move an object [C#] 1 Answer

Move camera with touch 0 Answers

Rotate GameObject on Touch along Y axis 1 Answer

Player animates with keyboard but not virtual joystick 0 Answers

iOS game "minimizes" if I touch the left side of the screen 0 Answers


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