- Home /
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 nothing 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 this for initialization
void Start () {
w = Screen.width;
}
// Update is called once per frame
void Update () {
int i = 0;
// Loop over every touch found
while (i < Input.touchCount)
{
// Is this 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;
}
}
}
can anyone post the complete script. I follow the instruction but could not run the it in my game
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.
This 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 this 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.
Thank you very much for your answer.
I used your script. I still have some Problem after doing these steps:
created a canvas
put my 2 buttons in my canvas
put an eventtrigger on my canvas (Not on the buttons) and set the trigger to listen on my buttons
created the script
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. $$anonymous$$aybe 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. :)
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 $$anonymous$$arma points, you get $$anonymous$$arma 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.
Yeah the Problem is fixed and everything is working now. Thank you.
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?
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?
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
I have the same problem and nobody gives me a solution