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,
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class move : MonoBehaviour {
-
-
- void Start () {
-
- }
-
-
- void Update () {
- transform.Translate (0.05f, 0f, 0f);
-
- }
- }
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.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class move : MonoBehaviour {
-
-
- void Start () {
-
- }
-
-
- void Update () {
- transform.Translate (-0.05f, 0f, 0f);
-
- }
- }
Save the program.
The sphere will move in a backward direction.
Up
Write the following code.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class move : MonoBehaviour {
-
-
- void Start () {
-
- }
-
-
- void Update () {
- transform.Translate (0.05f, 0.05f, 0f);
-
- }
- }
Save the program. Go to Unity and click on the Play button. The sphere will move upward.
Down
Use the following code.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class move : MonoBehaviour {
-
-
- void Start () {
-
- }
-
-
- void Update () {
- transform.Translate (0.05f, -0.05f, 0f);
-
- }
- }
Left
Write the following code.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class move : MonoBehaviour {
-
-
- void Start () {
-
- }
-
-
- void Update () {
- transform.Translate (0.00f, 0.00f, 0.05f);
-
- }
- }
Object move up, forward and left
Write the coding like the following to mono development.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class move : MonoBehaviour {
-
-
- void Start () {
-
- }
-
-
- void Update () {
- transform.Translate (0.001f, 0.01f, 0.01f);
-
- }
- }
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.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class move : MonoBehaviour {
-
-
- void Start () {
-
- }
-
-
- void Update () {
- if (Input.GetKey (KeyCode.W)) {
- transform.Translate (0.05f, 0f, 0f);
- }
- if (Input.GetKey (KeyCode.S)) {
- transform.Translate (-0.05f, 0f, 0f);
- }
- if (Input.GetKey (KeyCode.D)) {
- transform.Translate (0.0f, 0f, -0.05f);
- }
- if (Input.GetKey (KeyCode.A)) {
- transform.Translate (0.0f, 0f, 0.05f);
- }
- }
-
- }
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.