• 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 Eltore · Oct 02, 2014 at 04:01 PM · androidcameratransformvector3touch

Move to touch position

I wrote t$$anonymous$$s script to make the object move to the position, where the player touched. It gets the finger position correctly, but does not change it to world point value. If I were to use the original value, the object would move far away.

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
     public Vector3 realPos;
     public Vector3 tempPos;
     
     // Update is called once per frame
     void Update () {
         if(transform.position != realPos) {
             transform.position = Vector3.Lerp (transform.position, realPos, 0.3f);
         }
 
         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
             Vector3 fingerPos = Input.GetTouch(0).position;
 
             tempPos = fingerPos;
             tempPos.z = 0;
             realPos = Camera.main.ScreenToWorldPoint(tempPos);
             realPos.z = 0;
             Debug.Log (tempPos);
         }
     }
 }
 
Comment
Add comment · Show 1
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 Graham-Dunnett ♦♦ · Oct 02, 2014 at 04:02 PM 0
Share

1 Reply

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

Answer by robertbu · Oct 02, 2014 at 06:05 PM

There is a likely problem here. Your code:

         tempPos = fingerPos;
         tempPos.z = 0;
         realPos = Camera.main.ScreenToWorldPoint(tempPos);
         realPos.z = 0;
         Debug.Log (tempPos);

...will only work for an Orthograp$$anonymous$$c camera. And if want your object to have a 'z' value of 0.0. For a perspective camera, you need to assign the 'z' of tempPos the distance in front of the camera before you call ScreenToWorldPoint(). So if the camera was at -10 and you wanted to find the position at 'z = 0' (10 units in front of the camera):

 tempPos.z = 10.0;
 realPos = Camera.main.ScreenToWorldPoint(tempPos);

Note for your movement code, you should be using deltaTime.

  transform.position = Vector3.Lerp (transform.position, realPos, Time.deltaTime * 6.0);
 
  
     
Comment
Add comment · Show 2 · 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 kuos · Aug 26, 2015 at 11:40 PM 0
Share
avatar image AnisimovArtur kuos · Apr 28, 2019 at 11:44 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity Car Accelerometer 0 Answers

Why is my Camera rotation tilted after parenting it to head? 1 Answer

Transform Screen Point to Transform extension 1 Answer

2D Camera (Android, Iphone) 1 Answer

C# 2d Spawn Randomly along Camera Borders 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