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 Aapo f · May 04, 2015 at 11:12 AM · 2dscripting problemmovementtouch

My Touch Movement Script is Not Working Correctly

Im Making 2D game Where you must run away from enemies but i have problem with my movement script It moves only to Up and right i want it to move to left and down too Script:

 #pragma strict
 public var speed = 0.1F;
 function Update() {
     if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved) {
         // Get movement of the finger since last frame
         var touchPosition = Input.GetTouch(0).position;
         // Move object across XY plane
         transform.Translate(touchPosition.x * speed, touchPosition.y * speed, 0);
     }
 }
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 hbalint1 · May 04, 2015 at 11:19 AM

It's because you approached it wrong. The GetTouch(0).position gives back "Screen values" as coordinates. Like your bottom left corner of the device screen is (0,0) and the top right is (Screen.width, Screen.height). So wherever you touch it will always be positive x and y coordinates, that's why you move only right and up. You should store the touch's position on the begin of the movement (TouchPhase.Began), and then you can use the TouchPhase.Moved to determine whether the new coordinates were smaller or larger, than the originals (at he Began). So if the moved (x,y) coordinates are smaller, than the original (x,y) you should move down left. sorry but I can't provide you with code, just with the idea. maybe someone will come and get you one.

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 Aapo f · May 05, 2015 at 11:30 AM 0
Share

Sorry I dont understand what i need to do im new in unity

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Stop Moving When Not Touching Screen? 1 Answer

I Need Help With Mobile Touch Movement. 0 Answers

Move horizontally on touch 0 Answers

Move 2d game object instantly on position of Touch Input. 1 Answer

Object some times goes off 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