Moving a cube along the x and z axis

I’m trying to get a cube to move only along the x and z axis without user input. I have gotten them to move, but they are going through my ground and into the air. How do I get them to stay on the ground and move around inside my space? Hopefully that has enough information…

If you want it to stay on the ground I would say use a box collider and rigidbody . Also if you wanted to do a fixed animation (Animation that plays one animation and over and over again such as up and down) just make an animation. if you really want to do it by script here’s a code, but its simple. You can obv find more advanced code out there.

transform.position + new Vector3 (0, 5, 0) // Change value to fit your need

Here’s the code I’m using currently:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickUpMovement : MonoBehaviour {

    private Rigidbody rb;
    public float speed; 

    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody>();

    // Update is called once per frame
    void Update()

        // float moveHorizontal = 5f ;

        Vector3 movement = new Vector3(gameObject.transform.position.x + 5.0f, 0.0f, 4.2f);

        //rb.AddForce(movement * speed * Time.deltaTime);
        transform.position += new Vector3(speed * Time.unscaledDeltaTime, 0, 0);




I have now gotten it to move only along the ground; however, it is going through my walls.

In answer to your first question, I started learning Unity 2 weeks ago and for a class assignment, we are making a game that we can do whatever we want with. I’ve gone over Rigidbody briefly and barely brushed on the other two. I am a ninth grader working on a class project. I think I’d probably rather let physics do the work, I just want the cube to move randomly.