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 Gaegul2moja · Jan 21, 2019 at 06:23 AM · positiontouchscreenscreentoworldpointlog

How can I change touch location to map location?

I am trying to locate a cube object at the position where I touched with this code. However, the only result I can see is "Touched". I think ScreenToWorldPoint is not working. Did I made a mistake in this code?

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.touchCount > 0)
     {
         UnityEngine.Debug.Log("Touched");
         Vector2 pos_2 = Input.GetTouch(0).position;
         Camera camera = GetComponent<Camera>();
         Vector3 pos_3 = camera.ScreenToWorldPoint(pos_2);
         UnityEngine.Debug.Log("pos_3: "+pos_3);
         //Vector3 pos_3 = new Vector3(pos_2.x, pos_2.y, 0);
         transform.position = pos_3;
     }
 }

}

Comment
Add comment · Show 3
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 Gaegul2moja · Jan 21, 2019 at 06:43 AM 0
Share

I also have the same problem when I change 'if' to this. if (Input.Get$$anonymous$$ouseButtonDown(0)) { UnityEngine.Debug.Log("Touched"); this.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition); }

avatar image artofshashank · Jan 21, 2019 at 08:10 AM 0
Share

It seems you're trying to do a GetComponent() on the same object as the cube. That's probably returning a null ref and the code doesn't move forward. You can always call the camera like this:

 Vector3 pos_3 = Camera.main.ScreenToWorldPoint(pos_2);
avatar image Gaegul2moja artofshashank · Jan 22, 2019 at 04:31 AM 0
Share

oh thanks for the help! but i tried as you said but it's still not working....

// Update is called once per frame void Update () { if (Input.touchCount > 0) { UnityEngine.Debug.Log("Touched"); Vector2 pos_2 = Input.GetTouch(0).position; Vector3 pos_3 = Camera.main.ScreenToWorldPoint(pos_2); UnityEngine.Debug.Log("pos_3: "+pos_3); transform.position = pos_3; } }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sujitmarcus · Jan 22, 2019 at 11:58 AM

You are current asking for Touch on screen but if you need to Output "Touch" from touching a specific object on screen you have to learn about Raycasting. Here you can try.

  if (Input.touchCount == 1 && Input.GetTouch(0).phase == touchPhase) {
          Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
          RaycastHit hit;
          Debug.DrawRay(ray.origin, ray.direction * 100, Color.yellow, 100f);
          if(Physics.Raycast(ray, out hit))
          {
              Debug.Log(hit.transform.name);
              if (hit.collider.tag == "Cube" ) {
                                                           
                  Debug.Log("Touched" + hit.transform.name);

              }
          }
   }


Note: Don't Forget to assign "Cube" tag to your object in order to this code to work.

link text

@Gaegul2moja

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

123 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

Related Questions

Player position with script and screen sizes 0 Answers

ScreenToWorldCoordinates not accurate 1 Answer

How to interpret Event.current.delta? 0 Answers

Check if navmesh is at mouse position 0 Answers

Moving player relative to anywhere of the screen you are touching on 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