- Home /
Moving left and right touch problem
Hello guys, im making a simple game with a cube moving left and right when the user presses the left or right half of the screen. i put 2 invisible buttons on each side of the screen and the button actions are like this:
I have this code: this is under update:
//Left and Right buttons MOVEMENT
if (movingLeft == true) {
rb.AddForce (-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (movingRight == true) {
rb.AddForce (sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
and the other:
public bool MovingLeft
{
get
{
return movingLeft;
}
set
{
movingLeft = value;
}
}
public bool MovingRight
{
get
{
return movingRight;
}
set
{
movingRight = value;
}
}
//Not pressing - only forward force
public void SetVelocityZero()
{
movingLeft = false;
movingRight = false;
}
my problem is: When i press on the left button(cube goes left) and then without releasing it and press the right button i want the character to change direction. Now when im moving left i need to first release the button and then press the right ,Hello guys, im making a simple game with a cube moving left and right the code for my movind is this:
Update()
{
if (movingLeft == true) {
rb.AddForce (-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (movingRight == true) {
rb.AddForce (sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
}
public bool MovingLeft
{
get
{
return movingLeft;
}
set
{
movingLeft = value;
}
}
public bool MovingRight
{
get
{
return movingRight;
}
set
{
movingRight = value;
}
}
public void SetVelocityZero()
{
movingLeft = false;
movingRight = false;
}
and for movement im using invisible buttons placed on left half of screen and the right half of the screen. The buttons have this actions:
The thing im asking about is: When the user is holding left screen(the cube goes left) and without releasing it when he presses the right screen then the cube to change direction to right.
Umm... why not just use the StandardAssets.Joystick? I've made custom touch pads in the past though, if you want to do it, you need to access the Input.Touches array and deter$$anonymous$$e the touch location. Usually that's just dividing screen width by 2 and if the touch positions X axis is greater the divisor, it's on the right side, if it is less than the divisor, it is on the left side.
Does that make sense?
Your answer
Follow this Question
Related Questions
how to apply multiple Functionality like touch moves with Buttons move..? 0 Answers
MultiTouch issues 1 Answer
Unity Bug, Letting User To touch two UI Buttons as same time (simultaneously) 1 Answer
Unity InputField, how to select other GameObjects with ONE touch on mobile devices? 0 Answers
iPhone Multitouch Problem 1 Answer