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 /
  • Help Room /
avatar image
0
Question by jonahsrocket · Mar 31, 2016 at 09:33 AM · android2d gamemove an objecttouches

Move GameObject to touchpoint with one touch

Hi there,

I am new to Unity and starting to learn more and more. Here is a thing i can't figure out. I want my GameObject to move smoothly to the point where I touch the screen (Android). I found a few examples, but these move the GameObject as long as my finger touches the screen. But I want the object to move when I shortly touch the screen. I tried to change TouchPhase.Stationary to TouchPhase.Began but then the GameObject will only move a bit in the right direction.

Thank you in advance for answering!!!!

    void Update () {
      if (Input.touchCount > 0) {
          
          Touch touch = Input.GetTouch(0);
          
          if (touch.phase == TouchPhase.Stationary) 
     {
              Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));                
              transform.position = Vector3.Lerp(transform.position, touchPosition, Time.deltaTime);
          }
      }
  }
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

2 Replies

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

Answer by jonahsrocket · Apr 02, 2016 at 08:42 AM

@Ali hatem Thank you for the fast respons. I get an error saying touch does not contain a definition for 'position' and no extension method 'position' accepting a first argument of type 'Touch' could be found.

Any idea? I tried to figure this one out, but it's not working.

Comment
Add comment · Show 5 · 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 Ali-hatem · Apr 02, 2016 at 08:59 AM 0
Share

show me your script now i have tested my script with no problem.

avatar image jonahsrocket Ali-hatem · Apr 02, 2016 at 10:33 AM 0
Share

This is the code I use now. Thank you for taking the time!

 using UnityEngine;
 using System.Collections;
 
 public class TouchTest2 : $$anonymous$$onoBehaviour
 {
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     Vector3 myposition;
     void Update()
     {
         transform.position = Vector3.Lerp(transform.position, myposition, Time.deltaTime);
         if (Input.touchCount > 0)
         {
             Touch touch = Input.GetTouch(0);
             if (touch.phase == TouchPhase.Began)
             { 
                             myposition = touchPosition;
             }
         }
     }
 }
avatar image Ali-hatem jonahsrocket · Apr 02, 2016 at 10:46 AM 0
Share

where is this line of code :

Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));

ok i get it you have deleted the wrong line i mean delete the TouchPhase test so it will fallow doesn't matter how you touch so delete this ins$$anonymous$$d

 if (touch.phase == TouchPhase.Began){}
Show more comments
avatar image
0

Answer by Ali-hatem · Mar 31, 2016 at 02:56 PM

 Vector3 myposition;
 void Update(){
 transform.position = Vector3.Lerp(transform.position, myposition, Time.deltaTime);
     if (Input.touchCount > 0) {
         Touch touch = Input.GetTouch(0);
         Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
             myposition = touchPosition;
     }
  }
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

71 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

Related Questions

How can I use a 'grabber' to move objects in Unity (2D)? 1 Answer

How can I make an object point to a touch while pressing other buttons (2D for android) 1 Answer

Android double jump 0 Answers

Android not rendering Unity Objects (pink) 1 Answer

Move a game object when player rotates Unity 2.5D 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