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 jefvaia · Sep 12, 2018 at 05:59 PM · movement2d gameandriod

How to move an object towards the touch position?

Hello everyone, I'm trying to make a 2D game that can run on Android. I have a 2D sprite that I want to move towards the touch position. (the sprite is not exactly 2D, because the z-axis is -0.001). I have tried many scripts that i found online, but they aren't working. When i touch the screen, the sprite doesn't moves to the touch position, but it goes to random places. Here is one if the scripts that i tried: https://docs.unity3d.com/ScriptReference/Input.GetTouch.html. I hope you guys can help me.

Comment
Add comment · Show 6
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 tormentoarmagedoom · Sep 12, 2018 at 08:15 PM 0
Share

Eh.... Good day.

Learn to code, fropm the beginign. Dont want to simple copy psate code if dont know what it does...

avatar image jefvaia · Sep 13, 2018 at 05:26 AM 0
Share

I can understand what you are saying, but i have also tried self-made codes like this:`transform.position = new Vector2(Input.GetTouch(0).position.x, Input.GetTouch(0).position.y);` The problem with this code is that it will do the same things and it will place the z-axis to 0 and that means that the sprite will go behind the background.

avatar image Der-Die-Das jefvaia · Sep 13, 2018 at 06:52 AM 0
Share

For the problem that your sprite is behind other stuff. I think the best way to solve would be to play around with the Sprite Renderer's Order in Layer and Sorting Layer. If that doesn't work simply set the transform with a Vector3 like that

 transform.position = new Vector3(yourX, yourY, -0.001);

avatar image 3kWikiGames · Sep 13, 2018 at 05:02 PM 0
Share

Having the same problem but with a 3d object, no experience using screentoworldpoint so im very confused how to get it to follow my character.

avatar image Der-Die-Das 3kWikiGames · Sep 13, 2018 at 05:23 PM 1
Share

So you want a 3D Object to follow your touch input? If so, I think it would be the best if you quickly phrase a new question and I'll answer it if I see it (:

avatar image 3kWikiGames Der-Die-Das · Sep 13, 2018 at 05:28 PM 0
Share

That would be amazing I just posted it a little while ago on my account, was looking through to see if there were any questions I could possibly answer in the meantime. Thanks!

1 Reply

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

Answer by Der-Die-Das · Sep 13, 2018 at 06:48 AM

Your problem is probably that you do something like that:

 transform.position = Input.GetTouch(0).position;

Which is wrong since the position you get from the Input System is ScreenSpace and your transform Object is World Space. You might try something like:

 Camera cam = Camera.main;
 transform.position = cam.ScreenToWorldPoint(Input.GetTouch(0).position;

Be aware that this is untested code! And if you do that in Update you should probably cache the cam variable.

Hope that helps!

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 jefvaia · Sep 13, 2018 at 01:06 PM 0
Share

This was the code that i have tried, but wasn't working: transform.position = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position); It is similar to your code, but both codes did not work. Then I noticed that your code changes the position like it is a Vector2, but i need Vector3 because of the z-axis. Than i have changed the code to this:

 Camera cam = Camera.main;
 transform.position = cam.ScreenToWorldPoint(Input.GetTouch(0).position);
 transform.position = new Vector3(transform.position.x, transform.position.y, -0.0001f);

Now I have the right code, but thanks to your answer I now know what I did (partially) wrong. Thank for Helping me. Have a good day :)

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

155 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

Related Questions

Need help with Raycast2D 0 Answers

How to move along a few path follow ? 1 Answer

Move 2D Sprite Horizontally With Mouse 1 Answer

Moving a 2D Object a certain distance over time with a single button click 1 Answer

Multiple enemy prefabs moving towards a single goal. Need help! 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