Moving Objects using C# Scripts with Unity 3D

Unity3d is just one of the most widely used gaming engines. Unity windows, mac, linux, android, ios .. what you can do if you can support games that support. Now we will learn to move only one Cube object with the arrow keys or w, a, s, d keys.

First, let’s create an empty unity3d project. We should not add a package to empty our project.

Now let’s say GameObject -> 3D Object -> Plane from above. In the menu above you will see the GameObject tab.

How to give plane color ?

You can track the above way.Data then move the material you want to give the color.

Let’s say GameObject -> 3D Object -> Cube from above. In the menu above you will see the GameObject tab.


If you set color for cube , do the same for the plane.

Now let’s move on to our main goal;
The cube will be our character. We’re gonna move him now.


As seen in the picture, right-click on the Assets folder in our Project tab and click Create> Folder. Let’s call our folder Scripts so our project doesn’t get mixed up.
Now, right click on the newly created Scripts folder, and click Create> C # Scripts. C # The name of the script, let’s call it.

Double-click on our Moves.cs file and the MonoDevelop program within Unity3d will run. This is a collection program like Visual Studio. You can change these settings if you want. You can open your Scripts with another application.

Start:Start object start settings.

Update:The operations here are at each frame


transform: is the property that holds the movement values of our object.
position: position;

move.position = new Vector3 (0,0,0);
Here we give Cube a starting position. If we had not transformed our motion variable;

transform.position = new Vector3 (0,0,0);
We could have done this way.

With the method Input.GetAxis (): we have determined which direction to change. In other words, we have made it possible to move on that axis.
Vector3: The Vector3 class is used to define a 3-component vector.
Vector3.right returns the vector of the right side.
Speed: the variable speed of the object we define.
Time.deltaTime: let’s say simultaneously. We hit it with the frame to be at the same speed.

Our Translate method wanted the vector of a vector3 class.
I defined the speed of the public in the game so I can adjust.

Let’s get back to Unity. Let’s take the CubeMoving.cs file and put it on the Cube object.

Then, click on the Cube object and see if the Inspector screen shows the Scripts.

As you can see, I defined public variable Hiz. We can play from here. We’re done.
Let’s run our game. (I’m running like a screen in 16: 9 format.)

No problem. Right-left and up-down operations are running. We can even cross. But we’re leaving Camera. Now, I will say,
When we play 16: 9, the value of the game goes between -10, 10. We can see that easily. Then we should say:
If my object xacaki is less than -10, redirect it to x10.
If my object xacaki is greater than 10, then redirect it to x -10.
Obviously, two of us need to make this possible. Let’s write them down.

if (motion.position.x> 10) {
move.position = new Vector3 (-10, move.position.y, move.position.z);
else if (move.position.x <-10) {
move.position = new Vector3 (10, move.position.y, move.position.z);
We also do the same situation for our y value so that we are constantly on the object.

Our value is between - Let’s get him right now.

if (move.position.y> 7.5f) {
motion.position = new Vector3 (motion.position.x, -7.5f, motion.position.z);
else if (move.position.y <-7.5f) {
motion.position = new Vector3 (motion.position.x, 7.5f, motion.position.z);
So we kept the movement on the screen. And we could control our object with w, a, s, d and arrow keys.



if you have question do not forget to write from the chat button next to it or from the comment

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *