Move simple Object in Unity 2D

A slight improvement over Chris' answer:

transform.position = new Vector2(transform.position.x + movespeed * Time.deltaTime, transform.position.y);

Time.deltaTime the amount of time it's been between your two frames - This multiplication means no matter how fast or slow the player's computer is, the speed will be the same.


You can't assign the x value on position directly as it's a value type returned from a property getter. (See: Cannot modify the return value error c#)

Instead, you need to assign a new Vector3 value:

transform.position = new Vector3(transform.position.x + movespeed, transform.position.y);

Or if you're keeping most of the coordinate values the same, you can use the Translate method instead to move relatively:

transform.Translate(movespeed, 0, 0)

Tags:

C#

Unity3D