Transforming Objects Movement Using C# Scripts In Unity

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.   
  5. public class Movement : MonoBehaviour  
  6. {  
  7.     Vector3 Vec;  
  8.     // Start is called before the first frame update  
  9.     void Start()  
  10.     {  
  11.           
  12.     }  
  13.   
  14.     // Update is called once per frame  
  15.     void Update()  
  16.     {  
  17.   
  18.         Vec = transform.localPosition;  
  19.         Vec.y += Input.GetAxis("Jump") * Time.deltaTime * 20;  
  20.         Vec.x += Input.GetAxis("Horizontal") * Time.deltaTime * 20;  
  21.         Vec.z += Input.GetAxis("Vertical") * Time.deltaTime * 20;  
  22.         transform.localPosition = Vec;  
  23.     }  
  24. }  
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.   
  5. public class Movement : MonoBehaviour  
  6. {  
  7.      
  8.     // Start is called before the first frame update  
  9.     void Start()  
  10.     {  
  11.           
  12.     }  
  13.   
  14.     // Update is called once per frame  
  15.     void Update()  
  16.     {  
  17.           
  18.         if (Input.GetKey(KeyCode.UpArrow))  
  19.         {  
  20.             this.transform.Translate(Vector3.forward * Time.deltaTime);  
  21.         }  
  22.          
  23.         if (Input.GetKey(KeyCode.DownArrow))  
  24.         {  
  25.             this.transform.Translate(Vector3.back * Time.deltaTime);  
  26.         }  
  27.          
  28.         if (Input.GetKey(KeyCode.LeftArrow))  
  29.         {  
  30.             this.transform.Rotate(Vector3.up, -10);  
  31.         }  
  32.         
  33.         if (Input.GetKey(KeyCode.RightArrow))  
  34.         {  
  35.             this.transform.Rotate(Vector3.up, 10);  
  36.         }  
  37.   
  38.     }  
  39. }  
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.   
  5. public class Movement : MonoBehaviour  
  6. {  
  7.      
  8.     // Start is called before the first frame update  
  9.     void Start()  
  10.     {  
  11.           
  12.     }  
  13.   
  14.     // Update is called once per frame  
  15.     void Update()  
  16.     {  
  17.         if (Input.GetKey(KeyCode.A))  
  18.         {  
  19.             transform.Translate(0.1f, 0f, 0f);  
  20.         }  
  21.         if (Input.GetKey(KeyCode.D))  
  22.         {  
  23.             transform.Translate(-0.1f, 0f, 0f);  
  24.         }  
  25.         if (Input.GetKey(KeyCode.S))  
  26.         {  
  27.             transform.Translate(0.0f, 0f, -0.1f);  
  28.         }  
  29.         if (Input.GetKey(KeyCode.W))  
  30.         {  
  31.             transform.Translate(0.0f, 0f, 0.1f);  
  32.         }  
  33.     }  
  34. }  
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.


Similar Articles