Move until it touches wall

Hi.
I don’t know if this question is right in this categorie but…
Im making my first unity game and I have a problem.
I have a cube that can move with the arrow keys.
The cube is in a simple room.
The cube spawns at the right at the top in the room.
If I press down and then I immediately press up the cube turns around at the half of his way.
I don’t want this to be possible.
if I press the down key, I want the cube to go down until he touches the wall.
I’m not good at coding so i don’t know how to do this.

this is my Code now:

using UnityEngine;

public class MoveWithForceARROWS : MonoBehaviour
{
private Rigidbody _rigidbody;

[SerializeField] private float _movementForce = 10f;

private void Awake() => _rigidbody = GetComponent();

private void FixedUpdate()
{
if (Input.GetKey(KeyCode.UpArrow))
_rigidbody.AddForce(_movementForce * Vector3.forward);

if (Input.GetKey(KeyCode.DownArrow))
_rigidbody.AddForce(_movementForce * Vector3.back);

if (Input.GetKey(KeyCode.RightArrow))
_rigidbody.AddForce(_movementForce * Vector3.right);

if (Input.GetKey(KeyCode.LeftArrow))
_rigidbody.AddForce(_movementForce * Vector3.left);

}

}[/code]

The best way to do this in my opinion would be with a finite state machine. That sounds scary but it’s not. Check out my description here: How to know when Movement/Rotation is done

In your case, your states would be like “Moving Right”, “Moving Down”, “Moving Up”, “Moving Left”, “Waiting For Input”, and your state transitions would happen when the player hits a wall or presses a button during the waiting for input state.