Introduction

This article demonstrates how to move objects using C# scripts in Unity.
Prerequisites
Unity Environment version 2018.4.19f1

Create a New Project

Transforming Objects Movement Using C# Scripts In Unity

Create objects in Unity

First, you have to open the Unity project. Create the Plane for your game.
Transforming Objects Movement Using C# Scripts In Unity
Click on the "GameObject" menu in the menu bar. Select 3D objects and pick the "Cube" option.
Transforming Objects Movement Using C# Scripts In Unity
The cube will be added to the scene View.
Transforming Objects Movement Using C# Scripts In Unity
Drag and drop the Skyblue and Green material. The color of the Cube & Plane will be changed into Skyblue and Green.
Transforming Objects Movement Using C# Scripts In Unity
Click on the Play button. The cube will now appear in game View.
Transforming Objects Movement Using C# Scripts In Unity

Create C# Script

Right click on Assets. Select Create >> C# script.
Transforming Objects Movement Using C# Scripts In Unity
Rename the script as move.
Transforming Objects Movement Using C# Scripts In Unity
Write the predefined coding like the following,
(Jump,Horizontal,Vertical)
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Movement : MonoBehaviour
  5. {
  6. Vector3 Vec;
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. }
  11. // Update is called once per frame
  12. void Update()
  13. {
  14. Vec = transform.localPosition;
  15. Vec.y += Input.GetAxis("Jump") * Time.deltaTime * 20;
  16. Vec.x += Input.GetAxis("Horizontal") * Time.deltaTime * 20;
  17. Vec.z += Input.GetAxis("Vertical") * Time.deltaTime * 20;
  18. transform.localPosition = Vec;
  19. }
  20. }
Save the program.
Transforming Objects Movement Using C# Scripts In Unity
Go back to the Unity window. Drag and drop the move script onto the cube.
Transforming Objects Movement Using C# Scripts In Unity
Click on the Play button. Press "Up Arrow" Key, and the Cube will move in a forward direction.
Transforming Objects Movement Using C# Scripts In Unity
Press the "Down Arrow" Key, and the Cube will move in a backwards direction.
Transforming Objects Movement Using C# Scripts In Unity
Press the "Left Arrow" Key, and the Cube will move to the left.
Transforming Objects Movement Using C# Scripts In Unity
Press the "Right Arrow" Key, and the Cube will move to the right.
Transforming Objects Movement Using C# Scripts In Unity
Press the "Space" Key, and the Cube will jump.
Transforming Objects Movement Using C# Scripts In Unity
Move and Rotate the object by Arrow key press
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Movement : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. }
  10. // Update is called once per frame
  11. void Update()
  12. {
  13. if (Input.GetKey(KeyCode.UpArrow))
  14. {
  15. this.transform.Translate(Vector3.forward * Time.deltaTime);
  16. }
  17. if (Input.GetKey(KeyCode.DownArrow))
  18. {
  19. this.transform.Translate(Vector3.back * Time.deltaTime);
  20. }
  21. if (Input.GetKey(KeyCode.LeftArrow))
  22. {
  23. this.transform.Rotate(Vector3.up, -10);
  24. }
  25. if (Input.GetKey(KeyCode.RightArrow))
  26. {
  27. this.transform.Rotate(Vector3.up, 10);
  28. }
  29. }
  30. }
Save the program.
Press "Left & Right Arrow" Key, and the Cube will Rotate to the left and right.
Transforming Objects Movement Using C# Scripts In Unity
Press the "Up & Down Arrow" Key, and the Cube will move forward and backwards.
Transforming Objects Movement Using C# Scripts In Unity
Move the object by key press.
Write the code like the following.
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class Movement : MonoBehaviour
  5. {
  6. // Start is called before the first frame update
  7. void Start()
  8. {
  9. }
  10. // Update is called once per frame
  11. void Update()
  12. {
  13. if (Input.GetKey(KeyCode.A))
  14. {
  15. transform.Translate(0.1f, 0f, 0f);
  16. }
  17. if (Input.GetKey(KeyCode.D))
  18. {
  19. transform.Translate(-0.1f, 0f, 0f);
  20. }
  21. if (Input.GetKey(KeyCode.S))
  22. {
  23. transform.Translate(0.0f, 0f, -0.1f);
  24. }
  25. if (Input.GetKey(KeyCode.W))
  26. {
  27. transform.Translate(0.0f, 0f, 0.1f);
  28. }
  29. }
  30. }
Save the program.
Go to Unity window. Click on the Play button. Press the “W” key. The object will move forward.
Transforming Objects Movement Using C# Scripts In Unity
Press the “S” key. The object will move backwards.
Transforming Objects Movement Using C# Scripts In Unity
Press “D” key. The object will move to the right side.
Transforming Objects Movement Using C# Scripts In Unity
Press the “A” key. The object will move to the left side.
Transforming Objects Movement Using C# Scripts In Unity

Summary

I hope,you understood how to transform where objects move using C# scripts in Unity.