Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by IronGirl · Aug 11, 2014 at 09:41 AM · androidcamera-movement

Camera Movement and sprites Touch Unity2d android

Hi, I m developing an android 2d game , i m moving a camera with touch left , right , up and down when i touch anywhere to the device screen it's moving. and i have a sprite , when i click on it shows me a message that i m clicking on sprite. Here is the code that is attached to the camera:

 if (Input.touchCount > 0)
             {
                 touch = Input.GetTouch(0);
                 if (touch.phase == TouchPhase.Began)
                 {
                     startPos = touch.position;
                     fingerHold = true;
                 }
                 else if (touch.phase == TouchPhase.Moved)
                 {
                     endPos = touch.position;
                 }
                 else if (touch.phase == TouchPhase.Ended)
                 {
                     fingerHold = false;
                 }
             }
 
             if (fingerHold)
             {
 
                 float deltaX = endPos.x - startPos.x;
                 float deltaY = endPos.y - startPos.y;
                 bool horizontal = false;
 
                 if (Mathf.Abs(deltaX) > Mathf.Abs(deltaY))
                     horizontal = true;
 
                 if (horizontal)
                 {
                     if (deltaX < 0 && transform.position.x < CameraXLimitLeft)
                     {
                         transform.Translate(Vector3.left * Time.deltaTime * 20);
                     }
                     else if (deltaX > 0 && transform.position.x > CameraXLimitRight)
                     {
                         transform.Translate(Vector3.right * Time.deltaTime * 20);
                     }
                 }
                 else
                 {
                     if (deltaY < 0 && transform.position.y < CameraYLimitDown)
                     {
                         transform.Translate(Vector3.down * Time.deltaTime * 20);
                     }
                     else if (deltaY > 0 && transform.position.y > CameraYLimitUp)
                     {
                         transform.Translate(Vector3.up * Time.deltaTime * 20);
                     }
                 }
             }

And this is the script that attached to the sprite.

    public GUIText guiTextSprite;
             void OnMouseDown()
             {
                 Debug.Log("Test Sprite");
                 guiTextSprite.text = "Test Sprite";
                    
             }

PS: in the sprite there is a polygon collider2d , sprite renderer and rigidbody.

The problem that when i touch the sprite the camera still moves. I want when i touch the sprite the camera stop moving and when i touch anywhere to the screen the camera moves

Thanks for your help

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
1
Best Answer

Answer by IronGirl · Sep 02, 2014 at 02:57 PM

i find a solution for my problem here is the script attached to the camera to let the camera moves.

 #region Touch Mobile
 
         if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
         {
             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
             transform.Translate(-touchDeltaPosition.x * speed * Time.deltaTime, -touchDeltaPosition.y * speed * Time.deltaTime, 0);
 
             Vector3 tmpPosX = transform.position;
             tmpPosX.x = Mathf.Clamp(tmpPosX.x, -13.0f, 14.0f);
             transform.position = tmpPosX;
             
 
             Vector3 tmpPosY = transform.position;
             tmpPosY.y = Mathf.Clamp(tmpPosY.y, -7.0f, 18.0f);
             transform.position = tmpPosY;
            
         }
         #endregion 

this script let me move the camera when i touch the screen in the left , right , up and down and let me limit the movement of the camera in X and Y Axis.

for the Sprite here is the script.

 public float tapSpeed = 0.5f; 
  
         private float lastTapTime = 0;
         
          void OnMouseDown()
         {
         if((Time.time - lastTapTime) < tapSpeed)
         {
  
             Debug.Log("Double tap");
  
         }    
  
             lastTapTime = Time.time;
             
         }

This script is attached to the spriteand let you Double tap the sprite and when i double tap on sprite , the camera doesn't move.

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

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

Camera 2D Movement android unity 1 Answer

Android touches called the Input.GetAxis('Mouse X') 0 Answers

My game has low FPS on iPhone 5 but not Android... 0 Answers

How to display Interstitial Ads after a set number of plays 1 Answer

Cinemachine 2D move camera down on Button press 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