Moving left and right with one button in 2d Game
Hi, I am working on a runner game where you try to dodge obstacles. What I am trying to do is on a single key press it changes directions. So if it is going left then key press it goes left and vice versa. I made a code but the problem is that it won't switch directions. If you could, please look over and tell what is wrong with the code.
using UnityEngine; using System.Collections;
public class Main : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Rotate()
{
transform.Rotate(Vector3.back * -20);
}
void Update () {
int speed = 1;
int move = 1;
if (move == 1)
{
transform.Translate(Vector3.right * Time.deltaTime * speed);
}
if (move == -1)
{
transform.Translate(Vector3.left * Time.deltaTime * speed);
}
if(Input.GetKey("space"))
{
move = move * -1;
}
}
}
P.S. Later I want to make it an app. So how do I set it up so that if you tap the screen(anywhere on the screen) the character will switch directions. Thanks!
Answer by Mmmpies · Jun 12, 2016 at 07:50 AM
You need 2 bools for this...
private bool moveRight = true;
private bool buttonDown = false;
public void ButtonIsDown()
{
buttonDown = true;
}
public void ButtonIsUp()
{
buttonDown = false;
}
public void SwitchDirection()
{
moveRight = !moveRight;
}
void Update()
{
if (buttonDown && moveRight)
{
transform.Translate(Vector3.right * Time.deltaTime * speed);
} else if (buttonDown) {
transform.Translate(Vector3.left * Time.deltaTime * speed);
}
}
Then add a UI Button, you can get rid of the image and text part or put a canvas group and set Alpha to 0 (either way it'll make it transparent).
Add an event trigger to it and add 2 new event types, one for Pointer Down and one for Pointer Up.
In Pointer Down click + twice and drag the player with that script on it onto the two slots that appear. Then from the drop down select YourScriptName -> ButtonIsDown in one and YourScriptName -> SwitchDirection in the other.
In Pointer Up click + once and drag the play (with the script) onto the slot. From the drop down select YourScriptName -> ButtonIsUp.
You can test this in the editor as the UI accepts either touch or mouse clicks. You'll need your speed variable in there as well.
@$$anonymous$$mmpies Ok, I think I understand. I will mark your answer as the solution for now, and I will try it later because I have some work to do right now. But from the looks of it, the code you posted seems like it will work. Thanks!
So, I tested out your script, but I am stuck at the event trigger part. It wont let me drag the character to the event type, and what do you mean by "play(with the script)". Thanks!
Typo - I meant Player (with the script)
So put the script on the Player (your character) and then drag your character onto the slot.
Your answer
![](https://koobas.hobune.stream/wayback/20220612082748im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Player Dips Into Ground After Jump 0 Answers
C#: How do you make a 2D game object jump? 1 Answer
Checking if the player jumps while not grounded 0 Answers
Making a high score script 0 Answers