Problems with mobile touch movements.
I am trying to build a simple game on mobile, with two buttons, one to move left, the other to move right.
This is the script attached to my player:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Motor : MonoBehaviour {
public float moveSpeed = 5.0f;
private Rigidbody2D movement;
private void Start(){
movement = GetComponent<Rigidbody2D> ();
}
public void Move(char whichDir){
if (whichDir == 'L') {
movement.velocity = new Vector2 (-moveSpeed, 0);
}else if (whichDir == 'R') {
movement.velocity = new Vector2 (moveSpeed, 0);
} else {
movement.velocity = Vector2.zero;
}
}
}
This is the script attached to my left and right buttons:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class touchInput : MonoBehaviour {
private Motor playerController;
void Start(){
playerController = FindObjectOfType<Motor>();
}
public void LeftArrow(){
playerController.Move('L');
}
public void RightArrow(){
playerController.Move('R');
}
public void UnpressedArrow(){
playerController.Move('O');
}
}
The problem is that when I try to quickly switch between directions, my player just freezes. One other problem is that when I am holding one button down, then start holding down the other button, then release the button that I was holding first, the ball stops.
For example: I move my player left by pressing the left button, but want to switch directions to the right, so while still holding left, I begin to hold down right, then I release the left button. The ball then stops.
If you have ever played Run Bird Run, I would like to try to get my touch inputs to behave like it does in that game.
Your answer
![](https://koobas.hobune.stream/wayback/20220612123506im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unusual movement script. Any ideas how to do that? 0 Answers
Moving a ball left/right with buttons 0 Answers
Why is my ball flying away? 0 Answers
Swipe camera rotation controller (HELP) 0 Answers
Problem with Swipe and rb.MovePosition 0 Answers