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 FailureToAdapt · Nov 14, 2016 at 03:44 PM · c#androidunity 2dtouchscreen

Unity2d-Android-C#: How to convert current movement script to be used for touchscreen?

I am trying to figure out how to implement touchscreen for android. I want to be able to tap the left hand of the screen to go left while achieving the same type of movement I already have implemented through keyboard commands and tap the right side of the screen to go right with the same type of movement I already have through keyboard commands.

The code I am using for player movement is:

 using UnityEngine;
 using System.Collections;
  
 public class movement : MonoBehaviour
 {
  
     public Vector2 leftForce = new Vector2(-5, 5);
     public Vector2 rightForce = new Vector2(5, 5);
  
  
     // Update is called once per frame
     void Update()
     {
         // Jump Left
         if (Input.GetKeyDown("a"))
         {
             GetComponent<Rigidbody2D>().AddForce(leftForce, ForceMode2D.Impulse);
         }
  
         //Jump Right
  
         if (Input.GetKeyDown("d"))
         {
             GetComponent<Rigidbody2D>().AddForce(rightForce, ForceMode2D.Impulse);
         }
    }
 }

The code I have for movement is very simple, but I haven't been able to find the right tutorial that explains how to add touch functionality that uses this same type of code.

Thank you all for your time!

EDIT: If anyone needs any clarification please let me know. I am new to game programming and I figured I would start with something easy like a 2D game with VERY simple controls. I have a basic working model of the game on PC (without menus, point counters, etc), but I designed it for android.... and being the brilliant newbie I am... I didn't take into consideration that I would have to create new touch controls for the game.

I have done a lot of tutorials (I have spent two days... at least 8 hours on this), but none of them cover how to adapt my controls to a working touch interface. I have learned how to apply the CrossPlatformInput assets to my game, but it doesn't let me keep my movement script setup.

I have tried to setup my own UI Butttons, but I can't figure out how to make my script be the trigger for the buttons when pressed. I have changed the classes in my script to public, but I think I am so lost at this point that I really need someone to write the script with mine in mind and then help me by explaining each part.

Again, thank you all for at least reading my question. I really hope someone out there can take pity on this poor "little" (I am 36 years old..... ancient) unity hobby noob.

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

1 Reply

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

Answer by Naphier · Nov 14, 2016 at 09:57 PM

Age matters not my friend! You are nowhere near ancient! There are many ways to go. UI Buttons are a good choice. To get a button to fire a method you'll need to create a class that inherits from MonoBehaviour, attach that script to some object in your scene, then in the button component you'll add an OnClick event. Then you drag/drop the gameObject with the class you created and select the correct method from the dropdown list.

The tutorials are the perfect place to learn how to do these things. For UI: https://unity3d.com/learn/tutorials/topics/user-interface-ui You may specifically want to watch the one for Buttons.

If you're interested in one-on-one tutoring, then check out my site (naplandgames.com). I've taught students from age 16 to 50. I do highly suggest working through some tutorials first, though!

Cheers and best of luck!

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

276 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 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 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 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 to debug unity project while run in android device? 1 Answer

How to use External camera on andorid 0 Answers

Can not load the next scene 0 Answers

Using the free touchscreen Joystick my code cant detect the joystick 0 Answers

Unity 2d android game How to "freeze" position on Y axis 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