Introduction
This article demonstrates how to move objects using C# scripts in Unity.
Prerequisites
Unity Environment version 2018.4.19f1
Create a New Project

Create objects in Unity
First, you have to open the Unity project. Create the Plane for your game.

Click on the "GameObject" menu in the menu bar. Select 3D objects and pick the "Cube" option.

The cube will be added to the scene View.

Drag and drop the Skyblue and Green material. The color of the Cube & Plane will be changed into Skyblue and Green.

Click on the Play button. The cube will now appear in game View.

Create C# Script
Right click on Assets. Select Create >> C# script.

Rename the script as move.

Write the predefined coding like the following,
(Jump,Horizontal,Vertical)
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Movement : MonoBehaviour
- {
- Vector3 Vec;
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- Vec = transform.localPosition;
- Vec.y += Input.GetAxis("Jump") * Time.deltaTime * 20;
- Vec.x += Input.GetAxis("Horizontal") * Time.deltaTime * 20;
- Vec.z += Input.GetAxis("Vertical") * Time.deltaTime * 20;
- transform.localPosition = Vec;
- }
- }
Save the program.

Go back to the Unity window. Drag and drop the move script onto the cube.

Click on the Play button. Press "Up Arrow" Key, and the Cube will move in a forward direction.

Press the "Down Arrow" Key, and the Cube will move in a backwards direction.

Press the "Left Arrow" Key, and the Cube will move to the left.

Press the "Right Arrow" Key, and the Cube will move to the right.


Join the conversation! Your thoughts help the community grow.