Hello, I’m pretty new to unity and I’m trying to make an app game.
What I’m having trouble with right now, is I want the player object to move to the right without stopping when the space bar is pressed (or when the screen is touched when I port it to mobile). At the moment I can make it move when I hold down the space bar, but I want it to move constantly after one input without stopping.
Here is my code for movement:
{
public float _speed;
public float movementSpeed = 5.0f;
// Start is called before the first frame update
void Start()
{
transform.position = new Vector3(-2.03f, 0, 6f);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.DownArrow))
{
Vector3 position = this.transform.position;
position.y--;
this.transform.position = position;
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Vector3 position = this.transform.position;
position.y++;
this.transform.position = position;
}
if(Input.GetKey(KeyCode.Space))
{
transform.Translate(Vector3.right * Time.deltaTime);
}
if (transform.position.y >= 5f)
{
transform.position = new Vector3(transform.position.x, 4f, 6f);
}
if (transform.position.y <= -5f)
{
transform.position = new Vector3(transform.position.x, -4f, 6f);
}
}