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
0
Question by ironblock · Jul 28, 2014 at 02:39 PM · c#camera-movementswipe

swipe camera movement wont stay between limitations

when i touch my screen (800x480)the camera disappears. im trying to limit the area where my camera can go. i managed doing it with Clamp.

 if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved ) {
             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
             transform.Translate (touchDeltaPosition.x * Time.deltaTime * speed, touchDeltaPosition.y * Time.deltaTime * speed, 0);
 
             transform.position = new Vector3(    Mathf.Clamp(transform.position.x, minimumX, maximumX),    Mathf.Clamp(transform.position.y, minimumY, maximumY), transform.position.z);
         



old code :

 using UnityEngine;
 using System.Collections;
 
 public class MoveCam : MonoBehaviour {
 
 
     public float speed = 3;
 
 
 
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
     
         if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved ) {
             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
             transform.Translate (touchDeltaPosition.x * Time.deltaTime * speed, touchDeltaPosition.y * Time.deltaTime * speed, 0);
 
             if (Camera.main.transform.position.x >= 1f )
             {
                 Camera.main.transform.Translate (touchDeltaPosition.x =1f,0f,-10f);
             }
             if (Camera.main.transform.position.x <= -1f )
             {
                 Camera.main.transform.Translate (touchDeltaPosition.x =-1f,0f,-10f);
             }
             if (Camera.main.transform.position.y >= 1f )
             {
                 Camera.main.transform.Translate (touchDeltaPosition.y =1f,0f,-10f);;
             }
             if (Camera.main.transform.position.y <= -1f )
             {
                 Camera.main.transform.Translate (touchDeltaPosition.y = -1f,0f,-10f);
             }
 
                 
         
         }
 
     }
 }
 
Comment
Add comment · Show 4
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 robertbu · Jul 28, 2014 at 03:02 PM 0
Share

Touch positions are in Screen coordinates. Your camera lives in World coordinates. You need to convert between the two. There is not enough context here for me to give you the recipe for your specific situation. Start by researching Camera.ScreenToWorldPoint(). If the camera is perspective, make sure you understand how to set the 'z' value you pass to this function.

avatar image ironblock · Jul 28, 2014 at 03:11 PM 0
Share

i dont think the z coordinate actually maters. i have a pinch to zoom script. looking at the scripting API of ScreenToWorldPoint() i cant really figure out how to use it.

avatar image robertbu · Jul 28, 2014 at 03:31 PM 0
Share

If the camera is perspective, then you need to specify the distance in front of the camera to do the calculation. If you enter 0.0 for 'z', Camera.ScreenToWorldPoint() will return the position of the camera for all values. Note you don't have to position the camera at the 'z' value returned, but you need some distance in 'z' for a perspective.

avatar image ironblock · Jul 29, 2014 at 07:16 AM 0
Share

do i need to change the if statements aswell ?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nerull22 · Jul 28, 2014 at 03:05 PM

So I think where your problem is that you're directly relating the touch input into the transform of your camera, if I'm reading this correctly. When you touch, it's in screen positioning instead of world units.

So when you first press down and the touch phase is "Began" then record the position. Then convert that from screen units to world units. Camera.ScreenToWorldUnits I believe is the method, but check the unity script to make sure.

So when you move, convert the position to world units and then you can measure the world units instead of screen. Because right now, I believe when you swipe your sending your camera hundreds of world units out of position, which is why you're getting the effect.

Let us know if you're still having problems.

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

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

24 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

iTween Path changing 0 Answers

Having a main Camera rotate other cameras 2 Answers

Camera not being moved when dragged by mouse 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