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 /
avatar image
0
Question by ElucardL · Apr 18, 2019 at 10:21 AM · unity 5movementmobilemovetouchscreen

Can't make cube follow the finger.

Hello, I've just started scripting in unity and I'm a total noob. I need my cube follow a finger but not going up on y axis, that's all. Always follow a finger, but my version of code isn't working. My cube always going back to the start position. I searched for the info in internet, but after 2 hours of searching didn't find anything. All I found is Camera.main.ScreenToWorldPoint and I thought that the cube always returns to the start position because my touch is considered on the start screen and I should make it "WorldPoint" but nope, that didn't work. Help... Please.

 void Update()
 {
     if (Input.touchCount > 0)
     {
         
         Vector3 touch = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);

         if (Input.GetTouch(0).phase == TouchPhase.Stationary || Input.GetTouch(0).phase == TouchPhase.Moved)
         {
             
             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.x, 0, touch.z));
             transform.position = touchPosition;
         }
     }
 }

}

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

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

Answer by highpockets · Apr 18, 2019 at 12:07 PM

The touch.z needs to be the distance away from the camera that you want your object. If your camera is aligned/facing along the z axis. Get the z distance:

 float zDistance = cube.transform.position.z - camera.transform.position.z;


Then:

 Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.x, 0, zDistance));
              transform.position = touchPosition;

Comment
Add comment · Show 4 · 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 highpockets · Apr 18, 2019 at 12:14 PM 0
Share

You could also just get the Vector3.Distance(fromPosition, toPosition); which returns a float, but be careful of this in 3D perspective space because if an object is far out to the right or left, the object will use the camera as a pivot, essentially rotating around it. That’s why I would opt for getting the distance on the specific axis to eli$$anonymous$$ate the pivoting effect if the camera is stationary or at least doesn’t rotate

avatar image ElucardL highpockets · Apr 18, 2019 at 12:48 PM 0
Share

I got the idea, but for some reason it goes down left now, always. It was right up and back before. I just don't understand why :/

avatar image ElucardL highpockets · Apr 18, 2019 at 01:05 PM 0
Share

Oh, nope, Now I've got another problem but it worked. You solved my problem, thank you!! Code that I have now: void Update() { if (Input.touchCount > 0) {

         Vector3 touch = (Input.GetTouch(0).position);
         if (Input.GetTouch(0).phase == TouchPhase.Stationary || Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved)
         {
             float zDistance = cube.transform.position.z - camera.transform.position.z;
             Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.x, 0, zDistance));
             transform.position = touchPosition;
         }
     }
 }
avatar image highpockets ElucardL · Apr 18, 2019 at 03:40 PM 0
Share

Ok, I’ll convert it to an answer then, cheers

avatar image
0

Answer by qobion · Apr 18, 2019 at 10:39 AM

You are converting screen point to world 2 times. Remove first

 Vector3 touch = Input.GetTouch(0).position;

Comment
Add comment · Show 4 · 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 ElucardL · Apr 18, 2019 at 10:41 AM 0
Share

I tried that, not working

avatar image qobion · Apr 18, 2019 at 10:48 AM 0
Share
         Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch);
         touchPosition.y = 0;
         transform.position = touchPosition;
avatar image qobion qobion · Apr 18, 2019 at 10:51 AM 0
Share

Also touch position is vector2(x,y) So to convert it to point you should use all coordinates

avatar image ElucardL qobion · Apr 18, 2019 at 11:02 AM 0
Share

It's not working too. Still, cube just moving up back and right and with very high speed.

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

285 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 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 do I make a 3D character move and rotate (turn) the same direction they move? c# 0 Answers

How do I move an object with my finger?[C#] 1 Answer

Any ideas why my buttons work in the editor but not on mobile? 0 Answers

File size increases on different devices 1 Answer

Slowly move a GameObject on 1 axis, then destroy it. 1 Answer


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