Transforming Objects Using C# Scripts In Unity

Introduction

 
This article demonstrates how to move objects using C# scripts in Unity.
 
Prerequisites
 
Unity Environment version 5.6.1
 
Create objects in Unity
 
Step 1
 
First, you have to open the Unity project. Create the terrain, trees, and water for your game.
 

Click on the "GameObject" menu in the menu bar. Select 3D objects and pick the "sphere" option. The sphere will be added to the scene View.
 
 
Drag and drop the red material. The color of the sphere will be changed into red.
 
 
Click on the Play button. The sphere will now appear in-game View.
 
 
Create a C# Script
 
Step 2
 
Right-click on Assets. Select Create >> C# script.
 
 
Rename the script as a move.
 
 
Move the Object
 
Step 3
 
Double click on the move script. The MonoDevelop-Unity editor will open up.
 
 
Move the object forward
 
Write the coding like the following,
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class move : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.           
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     void Update () {  
  14.         transform.Translate (0.05f, 0f, 0f);  
  15.           
  16.     }  
  17. }  
Save the program.
 
 
Go back to the Unity window. Drag and drop the move script onto the sphere.
 
 
Click on the Play button. The sphere will move slowly in a forward direction.
 
 
Object moves backward
 
Write the code like the following.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class move : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.           
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     void Update () {  
  14.         transform.Translate (-0.05f, 0f, 0f);  
  15.           
  16.     }  
  17. }   
Save the program.
 
 
The sphere will move in a backward direction.
 
 
Up
 
Write the following code.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class move : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.           
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     void Update () {  
  14.         transform.Translate (0.05f, 0.05f, 0f);  
  15.           
  16.     }  
  17. }   
Save the program. Go to Unity and click on the Play button. The sphere will move upward.
 
 
Down
 
Use the following code.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class move : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.           
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     void Update () {  
  14.         transform.Translate (0.05f, -0.05f, 0f);  
  15.           
  16.     }  
  17. }  
Save the program.
 
 
Left
 
Write the following code.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class move : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.           
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     void Update () {  
  14.         transform.Translate (0.00f, 0.00f, 0.05f);  
  15.           
  16.     }  
  17. }  
Save the program.
 
 
Object move up, forward and left
 
Write the coding like the following to mono development.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class move : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.           
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     void Update () {  
  14.         transform.Translate (0.001f, 0.01f, 0.01f);  
  15.           
  16.     }  
  17. }  
Save the program. Go to Unity and click on the Play button. The sphere will be moving slowly to the right.
 
 
Move the object by keypress
 
Step 4
 
Write the code like the following.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class move : MonoBehaviour {  
  6.   
  7.     // Use this for initialization  
  8.     void Start () {  
  9.           
  10.     }  
  11.       
  12.     // Update is called once per frame  
  13.     void Update () {  
  14.         if (Input.GetKey (KeyCode.W)) {  
  15.             transform.Translate (0.05f, 0f, 0f);      
  16.         }  
  17.         if (Input.GetKey (KeyCode.S)) {  
  18.             transform.Translate (-0.05f, 0f, 0f);      
  19.         }  
  20.         if (Input.GetKey (KeyCode.D)) {  
  21.             transform.Translate (0.0f, 0f, -0.05f);      
  22.         }  
  23.         if (Input.GetKey (KeyCode.A)) {  
  24.             transform.Translate (0.0f, 0f, 0.05f);      
  25.         }  
  26.     }  
  27.   
  28. }  
Save the program.
 
 
Go to the Unity window. Click on the Play button. Press the “W” key. The object will be moved to the front.
 
 
Press the “S” key. The object will be moving back.
 
 
Press the “D” key. The object will move into the Right side.
 
 
Press the “A” key. The object will move into the left side.
 
 

Summary

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


Similar Articles