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 coolbird22 · May 07, 2014 at 05:53 PM · touchteleportended

Why is my object moving diagonally upwards and to the right instead of teleporting to touch position & how to make sure object teleports inside the screen and not outside the screen bounds ?

On TouchEnded, I wish my object to jump to the last position of the touch on the screen. On every TouchEnded, it jumps diagonally upwards and to the right for every touch that ends, and does not teleport to where the touch was. Also, how do I keep it from teleporting outside the screen, as after a few touches, it eventually moves outside the screen bounds. Here is my current code :

 public Camera camera;
 
 void Update(){
 
 if(Input.touchCount >0) {
         Touch touch = Input.GetTouch (0);
         if (Input.GetTouch(0).phase == TouchPhase.Ended) {
             Vector3 touchpos = Input.GetTouch(0).position;

              
             Vector3 createPos;
             createPos.x = Input.GetTouch(0).position.x;     
             createPos.y = Input.GetTouch(0).position.y;  
             createPos.z = 0;

             transform.Translate(createPos.normalized);
             
         }
     }
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by coolbird22 · May 08, 2014 at 06:15 PM

Managed to figure it out with some help from Reddit. Here is the code

     if(Input.touchCount >0) {
             Touch touch = Input.GetTouch (0);
             if (Input.GetTouch(0).phase == TouchPhase.Ended) {
                             
                 Vector3 inputPos = Input.GetTouch(0).position; 
                 inputPos.z = 0;
                 Vector3 worldPos = Camera.main.ScreenToWorldPoint(inputPos);
                 worldPos.z = 0f;
                 transform.position = worldPos;
         }
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 Jeff-Kesselman · May 08, 2014 at 06:24 PM 1
Share

Yup that will work IF your camera is fixed looking down the Z axis and you don't $$anonymous$$d the post-move z always being 0.

avatar image
0

Answer by Jeff-Kesselman · May 07, 2014 at 08:00 PM

TouchPos is going to be in screen coordinates, not world coordinates.

You can convert screen coordinates to world coordinates but you lose the Z which is important.

In order to do an exact match, you have to first transform the object's current position from world to screen coords, then replace the x and y in that point, then transform back.

eg

 Vector3 myObjectPos = Camera.main.WorldToScreenPoint(myObject.transform.position);
 myObjectPos.X = touchPoint.X;
 myObjectPos.Y = touchPoint.Y;
 myObjectPos = Camera.main.ScreenToWorldPoint(myObjectPos);
 myObject.transform.position = myObjectPos;
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 coolbird22 · May 07, 2014 at 08:18 PM 0
Share

I don't $$anonymous$$d losing the Z, as I'm working on a top-down concept. How do I go about changing the touchpos Vector to have transform.position ? Unity is giving errors that the touchpos Vector does not contain a defination for 'transform'.

 if(Input.touchCount >0) {
             Touch touch = Input.GetTouch (0);
             if (Input.GetTouch(0).phase == TouchPhase.Ended) {
                 Vector3 touchpos = Input.GetTouch(0).position;
 
                 Vector3 viewPos = Camera.main.WorldToScreenPoint(touchpos.transform.position); 
                 
                 viewPos.x = Input.GetTouch(0).position.x;     
                 viewPos.y = Input.GetTouch(0).position.y;  
                 
                 viewPos = Camera.main.ScreenToWorldPoint(viewPos);
                 touchpos.transform.position = viewPos;
                 
             }
         }
avatar image
0

Answer by rednax20 · May 07, 2014 at 08:13 PM

Your problem is line 15

   transform.Translate(createPos.normalized);

normalized will change the parameters in the vector to 1 if they are greater than 1, -1 if they are less than negative one and 0 if they are between 1 and -1.

CreatePos will always be equal to Vector3(1,1,0)

plus those coordinates on GetTouch(0).position are screen coordinates no world coordinates. so your object wouldn't even go to the right place if you got rid of normalized

you object would however go to the right place is you got rid of the .normalized if your camera was orthographic.

if your camera is perspective, well, i asked that question a while back. I ended up using plane.raycast, where you raycast a ray from your point.

heres the link to my question, although it was a bit more specific than yours, so it may not apply to you

http://answers.unity3d.com/questions/631530/get-touch-position-on-perspective-camera.html

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

22 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

Related Questions

Touch teleport script isnt working 2 Answers

Touch Phase ended not executing 1 Answer

Android touch teleport 1 Answer

Rotate Camera around Object 2 Answers

mouse input to touch input help please 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