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 Jort · Nov 17, 2014 at 04:31 PM · inputmovement script

how to convert keyboard controls into touch controls?

I am creating a 2d game that has the player move from left to right and the other way around on the ground. I am currently using this:

   if (Input.GetKey(moveLeft)) {
     rigidbody2D.velocity.x = 2;
    } else if (Input.GetKey(moveRight)) {
     rigidbody2D.velocity.x = -2;
    } else {
     rigidbody2D.velocity.x = 0;
    }
   

as code to move the object left and right. I've been googling a bit and found something about input.touch or so but nothing to get me going. I'd like some help on how to make this game so that when a player touches the object it will move along with the finger on the x axis. (so you can "slide" the object from left to right)

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 tanoshimi · Nov 17, 2014 at 04:33 PM 0
Share

What part of your question is not answered by https://www.google.co.uk/search?q=unity+touch+tutorial ?

avatar image Priyanshu · Nov 17, 2014 at 07:31 PM 0
Share

change the position of the object to the position of the touch. And restrict it in x-axis only.

avatar image Jort · Nov 18, 2014 at 01:02 PM 0
Share

using the following code:

 if (Input.touchCount > 0 && 
           Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved) {
         
             // Get movement of the finger since last frame
             var touchDeltaPosition:Vector2 = Input.GetTouch(0).deltaPosition;
             
             // $$anonymous$$ove object across XY plane
             transform.Translate (-touchDeltaPosition.x * speed, 
                         -touchDeltaPosition.y * speed, 0);
                         }

$$anonymous$$y object vanishes as soon as I touch the screen. where did I mess up?

(speed is set to 0.1)

2 Replies

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

Answer by Jort · Nov 18, 2014 at 08:35 PM

I found a solution:

     void Update () {
         if (Input.touchCount > 0) {
             // The screen has been touched so store the touch
             Touch touch = Input.GetTouch(0);
             
             if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved) {
                 // If the finger is on the screen, move the object smoothly to the touch position
                 Vector3 touchPosition = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10)); 
                 touchPosition.y = transform.position.y;
                 transform.position = Vector3.Lerp(transform.position, touchPosition, 20 * Time.deltaTime);
             }
         }
 
     
     }

This is the only script with which my object doesn't disappear. If you wish to enable movement on the y axis as well just delete "touchPosition.y = transform.position.y;" If you wish to increase the speed at which the object follows the touch just increase the number 20, for a lower speed you can ofcourse decrease the number.

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 psycocrusher · Nov 18, 2014 at 06:47 PM

I am using this:

https://www.assetstore.unity3d.com/en/#!/content/14695

it converts keyboard, touch, joysticks, vita etc...

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

28 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

Related Questions

Converting Input.getAxis("Horizontal") to touch screen input. 0 Answers

How to get Horizontal axis value to 0 instantly after releasing the positive button!!!! 0 Answers

How can I smooth out and unlock the movement with the input system? 0 Answers

Something weird with my input system 1 Answer

Cannot Jump While Moving Diagonally 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