# Moving object alone y-axis

I have look at other posts but I’m still confused as to how to do it
I want to move these bricks in a breakout game from position 3 to 2 and back and forth, starting from y-position 3.
I have never learned C# before and I’m just building with codes I got different places.
Here are my codes:

using UnityEngine;
using System.Collections;

public class MoveBricks : MonoBehaviour
{

``````void Update()
{
var vel = GetComponent<Rigidbody2D>().velocity;
var pos = GetComponent<Rigidbody2D>().position;
GetComponent<Rigidbody2D>().position = pos;
if (pos.y >= 3)
{
vel.y = -.5f;
GetComponent<Rigidbody2D>().velocity = vel;
}
if (pos.y >= 2f && pos.y <= 3)
{
GetComponent<Rigidbody2D>().velocity = vel;
}

if (pos.y <= 2f)
{
vel.y = .5f;
GetComponent<Rigidbody2D>().velocity = vel;
}

}
``````

}

These codes move my brick from 3 to around 4.6 then to 3.6 and back and forth from there.
Why?

The reason is you’re using Rigidbodys and their velocities. They are physics objects and have other forces acting on them like inertia and drag. Rather than the ‘go here, stop, and do this’ kind of mentality of a typical coding process they have the more physically-based properties of ‘for every action there is an equal and opposite reaction’. The bottom line for your purposes is that, assuming you don’t want to wrestle with the physics, you want to manipulate the GameObject’s transform directly instead. Something like this:

``````transform.position += Vector2.right * Time.deltaTime;
``````

Putting that in the update loop will move your block at one unit per second to the right. This kind of position manipulation is a lot easier than trying to fly around like a spaceship in my opinion.