• 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
0
Question by jason-yi-kai · May 21, 2013 at 06:21 AM · ioszoomclamppinchmove to

IOS zoom to touch position and clamp

Hi,

Currently, I have 2 questions here and wonder if anyone can help me. Thanks!

  1. I have trouble to make camera pinch zoom to touch position. 2

  2. I did a Mathf.clamp to limit the camera X and Y axis, however, it is very jaggy.

so here is my script. It does nice pinch to zoom and pan around while touch drag.

if(Input.touchCount == 2)

{

curDist = Input.GetTouch(0).position - Input.GetTouch(1).position;

preDist =((Input.GetTouch(0).position - Input.GetTouch(0).deltaPosition) - (Input.GetTouch(1).position - Input.GetTouch(1).deltaPosition)); touchDelta = curDist.magnitude - prevDist.magnitude;

Vector2 touch_pos = Input.GetTouch(0).deltaPosition/2;

//Drag Camera with 2 fingers.

transform.Translate((-touch_pos.x/dragSpeed) Camera.fieldOfView/2.5f), (-touch_pos.y/dragSpeed) Camera.fieldOfView/2.5f, 0f);

//Zoom

Camera.main.FieldOfView = Mathf.Clamp(Camera.main.fieldOfView - (0.1f * touchDelta), 20,60);

//Zoom to touch position (DIDN"T WORK!!!!!)

transform.position = Vector3.MoveTowards(transform.position, new Vector3(touch_pos.x, touch_pos.y, transform.position.z), 60 * Time.deltaTime);

//Clamp Camera.

transform.position = new Vector3(Mathf.Clamp(transform.position.x, -clampX, clampX),Mathf.Clamp(transform.position.y, -clampY, clampY),transform.position.z;

}

//Drag Camera with 1 finger.

if(Input.touchCount >= 1 && Input.touchCount <2)

{

Vector2 touch_pos = Input.GetTouch(0).deltaPosition/2;

transform.Translate((-touch_pos.x/dragSpeed) Camera.fieldOfView/2.5f), (-touch_pos.y/dragSpeed) Camera.fieldOfView/2.5f, 0f);

}

//Clamp Camera again (but still not smooth).

transform.position = new Vector3(Mathf.Clamp(transform.position.x, -clampX, clampX),Mathf.Clamp(transform.position.y, -clampY, clampY),transform.position.z;

your help will be appreciated. Thank you

Jason

Comment
Add comment · Show 1
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 jason-yi-kai · May 21, 2013 at 07:10 AM 0
Share

$$anonymous$$aybe if someone can tell me why my Camera Clamp is so jaggy? Is it because I can't allow player to drag while the script trying to clamp the camera at the same time?

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

13 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

Related Questions

How to centre a 3D pinch zoom 1 Answer

How to zoom and pan when you "pinch/swipe across screen" (iPhone) 5 Answers

Setting pivot to the middle of two touches - 2D project 0 Answers

Clamp player to screen borders 1 Answer

Zooming cam by finger gestures 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges