UnicMinds

Movement of Objects, Controlling Objects in Unity

Understanding Object Movements in Unity

Children will learn to make 2D and 3D games in the gaming platform, Unity, in our 3D Game Development course. During the course, the child will learn all the coding fundamentals of the programming language C#. 2D and 3D game development concepts are used in C# language. This is a short post about how an object in Unity can be controlled using arrow keys.

Step 1 : Create a New Project in Unity Hub

Creating a New Project in Unity Hub

Select Universal 3D template, give an appropriate name to the project and then click on create project as shown below.

Creating a new project in Unity

On clicking create project, it will open a new project.

3D Game Development in Unity

Step 2: Creating a plane as part of designing the game

3D Game Development in Unity, Moving a Cube Object in Unity 3D

Click on + under Hierarchy window, select 3D object and plane. It will create a plane as shown below.

Controlling 3D Object in Unity

You can use the colored arrow as shown above to move the plane as per the requirement.

Click on the scale tool as shown below to increase the size of the plan in any axis as required.

Unity Game Development Course

Click on the game view as shown below to see how the plane would look on running the game.

Unity Game Development 3D - Running the game

Step 3: Creating the cube 

Click on + under Hierarchy window, select 3D object and cube. It will create a cube as shown below.

Controlling 3D Cube in Unity, C#
C# and Game Development in Unity

You can use the colored arrow as shown above to move the plane as per the requirement.

Click on the scale tool as shown below to increase the size of the plan in any axis as required.

Unity Programming Course

Step 4: Creating a new script and attaching the script to the cube 

Go to the project window at the bottom, right click, select create -> scripts-> empty C# script

Programming in C# on Unity
Unity Programming

Rename the script as per your requirement. 

Drag the script and place the mouse on the cube. It will attach the script to the cube as shown below.

Unity Game Development - Attaching C# script to an object

Step 5: Writing the code in the script

Double click on the script created , it will open the visual studio editor.

Programming in C# in Unity Engine

The script will have two inbuilt functions start() and update() as shown above.

The code written inside the start function will be executed once whereas the code written inside the update() function will be executed forever until the execution of the code is stopped.

Write the below code in the update() function  and save the code.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class movecube : MonoBehaviour

{

    // Start is called before the first frame update

    void Start()

    {

    }

    // Update is called once per frame

    void Update()

    {

        if (Input.GetKey(KeyCode.LeftArrow))

        {

            transform.Translate(-0.2f, 0, 0);

        }

        if (Input.GetKey(KeyCode.RightArrow))

        {

            transform.Translate(0.2f, 0, 0);

        }

        if (Input.GetKey(KeyCode.UpArrow))

        {

            transform.Translate(0, 0.2f, 0);

        }

        if (Input.GetKey(KeyCode.DownArrow))

        {

            transform.Translate(0, -0.2f, 0);

        }

    }

}

Explanation of the code:

 if (Input.GetKey(KeyCode.LeftArrow))

        {

            transform.Translate(-0.2f, 0, 0);

        }

The above code checks if the left arrow key is pressed on the keyboard. If it is pressed, move the cube in the negative x axis by -0.2. It will move the cube by -0.2 from right to left from the original position of the cube.

Step 6: Run the code and check 

After saving the code, come back to unity and click on run button on the top to check if the code is working.

You should be able to move the cube using left, right, up and down arrow keys.

Hope this is useful, thank you.

You may like to read: Can we trust jpg files?, Most popular FPS games in Roblox, & Intrusion Detection Systems

Leave a Comment

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

BOOK A FREE TRIAL