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 /
This question was closed Sep 05, 2018 at 09:52 AM by Joheil for the following reason:

La pregunta fué respondida y una respuesta ha sido aceptada

avatar image
0
Question by Joheil · Oct 23, 2016 at 08:53 AM · androidunity 5touch controlstouchscreenarcade

Vector3.back not working

Hi, I'm doing a 2D arcade game for Android devices and I'm having an issue with Vector3.back. What I'm trying to achive is that the player avatar moves to the part of the screen that the player had touched. Vector3.left, .right and .forward are working nice but when I use Vector3.back or -Vector3.forward the object doesn't moves to the touched position. The script I'm using is the following one (if someone knows a better and cheaper way to do this I'd be pleased to know :D):

     if (mobileControls == true) {

         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary) {

             double halfXScreen = Screen.width / 2.0;
             double halfZScreen = Screen.height / 2.0;
             Vector3 touchPosition = Input.GetTouch(0).position;

             if(touchPosition.x < halfXScreen) { //works nice
                 gameObject.transform.Translate(Vector3.left * speed * Time.deltaTime);
             } 
             else if (touchPosition.x > halfXScreen) { //works nice
                 gameObject.transform.Translate(Vector3.right * speed* Time.deltaTime);
             }

             if(touchPosition.z < halfZScreen) { //works nice
                 gameObject.transform.Translate(Vector3.forward * speed * Time.deltaTime);
             } 
             else if (touchPosition.z > halfZScreen) { //doesn't works :(
                 gameObject.transform.Translate(Vector3.back * speed * Time.deltaTime);
             }
         }

I'm using Unity 5.4.1f1 Personal Edition (64 bits)

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by saschandroid · Oct 24, 2016 at 09:30 PM

Input.GetTouch(0).position gives you a Vector2. With Vector3 touchPosition = Input.GetTouch(0).position; you turn it into Vector3, where the z-component of the vector will always be 0. So touchPosition.z < halfZScreen is always true and touchPosition.z > halfZScreen always false and therefor the translation in Vector3.back direction is never called. Use touchPosition.y instead of touchPosition.z and see if it works for you.

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
avatar image
0

Answer by Joheil · Oct 25, 2016 at 08:33 AM

It works!!!! Thank you very much saschandroid! Do you know a cheaper way to doo the same or how can I use the default movement inputs (horizontal and vertical) to do the same?

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 saschandroid · Oct 25, 2016 at 09:18 AM 0
Share

You could use deltaPosition for movement inputs (see first example here).

Follow this Question

Answers Answers and Comments

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

How to replace KeyCode.Space with a simple tap on Android? 1 Answer

How can convert keyboard controller to touchscreen(android .ios ...etc) 1 Answer

Unity 2d android game How to "freeze" position on Y axis 1 Answer

Touchscreen Movement?? 1 Answer

i want to move object only when i swipe on screen not when touch, Unity 2d Android 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