• 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
Question by Tokimimo · Feb 28, 2015 at 05:34 PM · androidmovementinputcharactertouch

Moving character with touch buttons (Android)

hello folks,

I wanted to start my first smartphone project with touch responsibility. I added a character with a rigibody2D attached to it and wrote a script, attached to the charater, too. Now my problem is he doesnt react and not$$anonymous$$ng is triggered at all. If you can show me my problem or how I can do it with two UI buttons to move left or right I would really Appreciate it.

My Code

 using UnityEngine;
 using System.Collections;
 
 public class movement : MonoBehaviour {
 
     float forwardSpeed = 10f;
     private float w;
 
     // Use t$$anonymous$$s for initialization
     void Start () {
         w = Screen.width;
     }
     
     // Update is called once per frame
     void Update () {
         int i = 0;
         
         // Loop over every touch found
         w$$anonymous$$le (i < Input.touchCount)
         {
             // Is t$$anonymous$$s the beginning phase of the touch?
             if (Input.GetTouch(i).phase == TouchPhase.Began)
             {
                 Debug.Log ("Triggered: " + Input.GetTouch(i).position.x);
                 // Does the touch happens on the right side of the screen?
                 if (Input.GetTouch(i).position.x > w / 2) {
                     // Move your character right
                     Debug.Log ("Triggered: " + Input.GetTouch(i).position.x);
                     rigidbody2D.AddForce (Vector2.right * forwardSpeed);
                     
                 }
                 
                 // Does the touch happens on the left side of the screen?
                 if (Input.GetTouch(i).position.x < w / 2){
                     Debug.Log ("Triggered: " + Input.GetTouch(i).position.x);
                     // Move your character left
                     rigidbody2D.AddForce (Vector3.left * forwardSpeed);
                     
                 }
             }
             ++i;
     }
 }
 }

Comment

People who like this

0 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 Blackraiderx2 · Mar 05, 2017 at 10:01 AM 0
Share

can anyone post the complete script. I follow the instruction but could not run the it in my game

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Mmmpies · Feb 28, 2015 at 05:49 PM

OK I reformatted your code be careful with that as it makes it harder to read and probably one of the reasons you were in moderation for so long.

T$$anonymous$$s is easy with the new UI, put a canvas with a left and right button on it.

Add Component -> Event Trigger for both and add both a PointerEnter and PointerExit (or PointerDown/PointerUp) should be the same for touch devices.

Now create a script that has 2 public functions that can be accessed by you pointer events. That script can either be move once or (and t$$anonymous$$s is why we also add PointerExit/PointerUp) set a bool to true so it moves all the time if that bool is true:

 using UnityEngine.UI;   // add to the top
 
 private bool moveLeft;
 private bool moveRight;
 
 void Update()
 {
     if(moveLeft && !moveRight)
         rigidbody2D.AddForce (Vector3.left * forwardSpeed);
 
     if(moveRight && !moveLeft)
         rigidbody2D.AddForce (Vector2.right * forwardSpeed);
 }
 
 public void MoveMeLeft()
 {
     moveLeft = true;
 }
 
 public void StopMeLeft()
 {
     moveLeft = false;
 }
 
 // do the same for right

Drag that script onto say the canvas and in the event trigger(s) click + and a slot appears. Drag the canvas (with the script on it) onto the slot and then from the dropdown select MyScriptName -> MoveMeLeft for the Left button PointerEnter/PointerDown and well you get the idea just add each public function to the correct event on the button.

Comment
Tokimimo
wezerds
Mohamed Elmekkawy
sir.munches
elwiz
AttentionHorse

People who like this

6 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 Tokimimo · Mar 04, 2015 at 07:45 PM 1
Share

Thank you very much for your answer.

I used your script. I still have some Problem after doing these steps:

  1. created a canvas

  2. put my 2 buttons in my canvas

  3. put an eventtrigger on my canvas (Not on the buttons) and set the trigger to listen on my buttons

  4. created the script

  5. put the script on my two buttons

Now the character is still not moving, maybe I should say that I use custom images and no buttons. Maybe these need to be accessed different. On my previous project I just said listen to an touch no matter where and then do x. But I used mouseclick(0) for it because an touch is interpreted as mouseclick(0) I read somewhere.

I get the idea of the controlling now but I think the problem is that I first need to get the Gameobject. I can't controll it by just saying rigibody2D on some script not attached to the character with the rigibody, or am I wrong?

PS: Sorry for my bad english but I'm from Germany :)

//Edit:

I just searhed for an GameObject with the tag "Player" and said "player.rigibody2d.addforce". Now the second problem I needed to fix was the eventtrigger. You need to put the eventtriger on the button itself and not on the canvas. Now my Character is moving and I can finally continue :)

Thank you. :)

avatar image Mmmpies · Mar 04, 2015 at 09:07 PM 0
Share

No problem, I think you're saying that it's fixed, your English is way better than my German :¬) but it's still not 100% clear.

If it is fixed click the tick button for many reasons, I get Karma points, you get Karma points and more importantly the question gets closed down so others don't look at it to try and answer and anyone with similar issues can find a possible answer here.

If it's not fixed let me know what issue you still have and I'll try and help, but there's a problem with me getting unity e-mails at the moment so if you need help look on the forum for me and start a conversation there, hopefully that'll get sent to me.

avatar image Tokimimo · Mar 11, 2015 at 09:15 PM 0
Share

Yeah the Problem is fixed and everything is working now. Thank you.

avatar image SpontaneousIllusion · Feb 19, 2016 at 02:56 PM 0
Share

Hi, I have been looking through your code and re purposing it for myself. I have the character now moving left and right when the character is spawned in. However I can't seem to actually make the character switch between whether he is moving left or right, he just keeps moving within one direction any idea how to fix this?

avatar image darkja · May 11, 2017 at 11:36 AM 0
Share

hello :)

I followed the steps in your answer "http://answers.unity3d.com/questions/911698/moving-character-with-touch-buttons-android.html" it works but my player takes a long time to switch between left and right movements, how can i fix it and make it smoother and faster?

@mmmpies

avatar image

Answer by Blackraiderx2 · Mar 05, 2017 at 03:25 PM

can anyone post the complete script. I follow the instruction but could not run the it in my game

Comment
MatiaszHarder

People who like this

1 Show 1 · 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 MatiaszHarder · Apr 08, 2017 at 02:00 AM 0
Share

I have the same problem and nobody gives me a solution

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting My Character to Move 1 Answer

Help with touchscreen controls 2 Answers

How to make a character move towards a side of the screen that's pressed at a constant rate? 2 Answers

Input.GetAxis("Vertical") on touch devices. 2 Answers

Input.GetTouch(0).position.x and TouchPhase.Began 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