How to get a rigidbody to move left and right?

Hey Programmers
I’m making a simple game. And since I’m pretty new to programming I need some help :smiley:
How do i make a rigidbody move left and right. Not forward and backwards. Just left and right :smiley:

But I dont know how to write the script for the function…
Help please?

Thank you in advance :smiley:

using UnityEngine;
using System.Collections;

public class LeftAndRightTest : MonoBehaviour {

    public float speed = 6.0F;
    private Vector2 moveDirection = Vector2.zero;
    void Update() {
        CharacterController controller = GetComponent<CharacterController>();
        if (controller.isGrounded) {
            moveDirection = new Vector2(Input.GetAxis("Horizontal"), 0);
            moveDirection = transform.TransformDirection(moveDirection);
            moveDirection *= speed;
            
        }
        controller.Move(moveDirection * Time.deltaTime);
    }
}

This should work. No guarantees. This does require a character controller on the object that the script is on.