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
5
Question by monserboy · Feb 14, 2014 at 06:01 PM · android2dtransformtouchvector2

Move object by touch input

Hi

I'm trying to implement a touch function into my game which lets the player object move to the left or to the right (2D game) depending on the x position the user touches on the screen.

So for example, if the user presses for a long time on the left side of the screen (-x coordinate) the player object moves to the left.

I have the following code in C# but it doesn't respond to my touches:

 using UnityEngine;
 using System.Collections;
 
 public class Touch : MonoBehaviour {
  
     public GameObject player;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;
 
             //Check if it is left or right?
             if(touchDeltaPosition.x < 0.0f){
                 player.transform.Translate(Vector3.left * 10 * Time.deltaTime);
             } else if (touchDeltaPosition.x > 0.0f) {
                 player.transform.Translate(Vector3.right * 10 * Time.deltaTime);
             }
 
         }
     }
 }
 

Please note that I'm new to Unity.

Thanks for your help in advance!

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 falconer · Oct 12, 2014 at 04:02 AM 0
Share

This article by The Game Contriver helped me solve this... $$anonymous$$ove Object To Tap/Click Position

avatar image $$anonymous$$ · Jun 08, 2015 at 07:34 AM 0
Share

How to make the gameobject up down left and right?

avatar image $$anonymous$$ · Jun 08, 2015 at 10:16 AM 0
Share

And how to move up and down?

5 Replies

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

Answer by Maui-M · Feb 14, 2014 at 06:36 PM

Delta position is the change in position since the last frame. You want the position of the touch instead.

Your if left/right statements will need to be changed as well since touchPosition will now be in pixels instead of world position.

Replace:

 Vector2 touchDeltaPosition = Input.GetTouch(0).deltaPosition;

With:

 Vector3 touchPosition = Input.GetTouch(0).position;
Comment
Add comment · Show 4 · 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 fafase · Feb 14, 2014 at 06:42 PM 0
Share

Those are vector2, position is vector3. Something is wrong. Being in 2D still requires Vector3 for position.

avatar image Maui-M · Feb 14, 2014 at 07:00 PM 0
Share

Your right, updated the code. You can just use a Vector3, it will just have a z value which you wouldn't have any use for.

avatar image robertbu · Feb 14, 2014 at 07:09 PM 0
Share

You don't need Vector3, but you do need to change from 'deltaPosition' to 'position' as indicated by @$$anonymous$$aui $$anonymous$$. Then you are going to have to compare against Screen.width / 2.0 rather than 0.0 to deter$$anonymous$$e left vs. right side of the screen. Screen coordinates start at (0,0) in the lower left of the screen.

avatar image monserboy · Feb 16, 2014 at 06:48 PM 0
Share

Thank you very much guys, I've fixed it! The Unity3D community rocks :)

avatar image
6

Answer by monserboy · Feb 16, 2014 at 06:59 PM

Just in case someone is wondering how I managed to fix this. I used the following code. This code is not optimized but it shows you how to get started:

 using UnityEngine;
 using System.Collections;
 
 public class Touch : MonoBehaviour {

     public GameObject player;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
         {
             Vector2 touchPosition = Input.GetTouch(0).position;
             double halfScreen = Screen.width / 2.0;
 
             //Check if it is left or right?
             if(touchPosition.x < halfScreen){
                 player.transform.Translate(Vector3.left * 5 * Time.deltaTime);
             } else if (touchPosition.x > halfScreen) {
                 player.transform.Translate(Vector3.right * 5 * Time.deltaTime);
             }
 
         }
 
     }
 }
 
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
avatar image
0

Answer by malcolmmaima · Jan 24, 2017 at 03:40 PM

Been struggling with something similar for a while now, Your code works perfectly!!! Thank you!!

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
avatar image
0

Answer by gararules112 · May 14, 2018 at 02:26 PM

How would you clamp this so that it cannot go past a certain area or range on the x axis? @monserboy

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
avatar image
0

Answer by CrazyAli · Oct 07, 2019 at 06:35 PM

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class ObjectTouchMove : MonoBehaviour {

 private Vector3 _touchPosition;
 private Rigidbody2D _rb;
 private Vector3 _direction;
 private float _moveSpeed = 10f;

 // Use this for initialization
 void Start () {
     _rb = GetComponent<Rigidbody2D>();
 }
 
 // Update is called once per frame
 void Update () {
     if (Input.touchCount > 0)
     {
         Touch touch = Input.GetTouch(0);
         _touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
         _touchPosition.z = 0;
         _direction = (_touchPosition - transform.position);
         _rb.velocity = new Vector2(_direction.x, _direction.y) * _moveSpeed;

         if (touch.phase == TouchPhase.Ended)
         {
             _rb.velocity = Vector2.zero;
         }
     }
 }

}

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

24 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

Related Questions

Unity Car Accelerometer 0 Answers

Camera swipe AND gameObject touch (Android) 1 Answer

How to properly implement snake style controls for android? 0 Answers

How to continuously detect the exact location of a touch on Android? 1 Answer

Dragging a 2D sprite with touch 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