- Home /
how to make a block move in one direction,Make a Object such as a Block move in one direction
I'm new to 2d game creating and coding, i have a png tumbleweed and want it to move in one direction, how would i go about doing this?,Im completely new to creating a game or coding, i have a png tumble weed which i want just to move in one direction. how do i do this? what do i need to add to the tumble weed? any wanders would be awesome!
Answer by unity_ek98vnTRplGj8Q · Dec 16, 2019 at 07:57 PM
I would recommend checking out the Unity tutorials (lean.Unity.com). These are great starting points for both 3D and 2D games and will teach you a lot of the basic aspects of how Unity works.
To answer your question specifically, make sure that you have a Game Object in your scene with the tumbleweed image attached. Once you have that, select it and click "Add Component" in the Inspector window and click "New Script", call it whatever you like, and open it. You will see something like this:
public class NewScript : Monobehavior
{
void Start(){
}
void Update(){
}
}
The Update()
function will be called every frame. If you simply want to move in one direction, for example the X direction, try this.
public class NewScript : Monobehavior
{
public int speed;
void Start(){
}
void Update(){
transform.Translate(new Vector3(speed*Time.deltaTime,0f,0f));
}
}
Now every frame your object will move in the X direction by whatever speed value you set (you can set this in the inspector) and scaled by the framerate you are running at.