Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Fickum · Mar 22, 2020 at 12:40 PM · vector3touch controlsscreentoworldpointmovment

problem with ScreenToWorldPoint (keep getting the same coordinates)

I wrote a code to move my player to the touch position :

 public class Movment : MonoBehaviour
 {
     public Vector2 touch;
     public Transform Player;
     new Vector3 vec;
 
     // Update is called once per frame
     
     void Update()
     {
         for (int i = 0; i < Input.touchCount; i++)
         {
             touch = Camera.main.ScreenToWorldPoint(Input.touches[i].position);
             vec = new Vector3(touch.x, touch.y, 5);
             Debug.DrawLine(Vector3.zero, vec, Color.red);
             Debug.Log(vec);
             Player.position = vec;
         }   
     }
 }

it keeps giving me (-3.7, 3.5, 5.0) I spent 2 hours reading solutions and answers (like setting a z coordinate to the vector3) but nothing solved it

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
1

Answer by Namey5 · Mar 23, 2020 at 02:27 AM

You are passing a Vector2 (touch.position) to ScreenToWorldPoint, which takes a Vector3 input. This will end up casting to a Vector3 with an assumed z-coordinate of 0, which means the function will always return the camera's position regardless of the screen point. If you want to find the world space position of a point on screen with a set z-position, you can use a plane-raycast;

 //This is the world space z-coord of the points
 float zPosition = 5f;
 
 if (Input.touchCount > 0)
 {
     //Z-Position is irrelevant with ScreenPointToRay
     Ray ray = Camera.main.ScreenPointToRay (Input.GetTouch (0).position);
     //Create a new plane on the z-axis
     Plane plane = new Plane (new Vector3 (0f, 0f, -1f), new Vector3 (0f, 0f, zPosition));
 
     //Raycast against the plane
     if (plane.Raycast (ray, out float hit))
     {
         //Find the world pos of our screen point on the plane
         Vector3 hitPos = ray.GetPoint (hit);
         Player.position = hitPos;
     }
 }
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

153 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

Related Questions

Touch.ScreenPosition doesnt work ? 0 Answers

Problem with ScreenToWorldPoint and touches (C#) 2 Answers

Camera ScreenToWorldPoint Instatiating 1 Answer

Trying To Find Mouse Position On Tap 1 Answer

How to keep the same position of the object instead of camera 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