Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Eltore · Oct 02, 2014 at 04:01 PM · androidcameratransformvector3touch

Move to touch position

I wrote this 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

Do a Raycast to get a world position.

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 Orthographic 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

I get an error saying that argument #3 cannot convert double to expression to type float. I assume time.deltatime is in double units?

avatar image AnisimovArthur kuos · Apr 28, 2019 at 11:44 AM 0
Share

Just add 'f' to value. transform.position = Vector3.Lerp (transform.position, realPos, Time.deltaTime * 6.0f);

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Unity Car Accelerometer 0 Answers

How do i lock the position of the camera above the player relative to the origin point? 0 Answers

How can i make my crosshair drag behind the camera? 0 Answers

Control the camera with a half of the touch screen 0 Answers

GearVR rotation stutter InputTracking.GetNodeStates 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