Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity

Introduction

 
In this article, we discuss how to make mobile touch controls and move a character from left to right using C# scripts in Unity.
 
Prerequisites
 
Unity Environment version 2018.4.19f1 
 
Create the 2D project in Unity.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Now open up main.Assets. Import the 2D Character for the Unity. Drag the Scene view onto your view controller.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Select 2D Character and go to the Drag n Drop Scene view in Unity.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Create a Button in your project.
 
Click on the "GameObject" menu in the menu bar. Select Create UI (Button). The Create Button will be added to the scene View.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Rename the Button as Left button.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Create a DuplicateButton in your project.
 
Select the button and type the keys (Ctrl + D). The Create Duplicate Button will be added to the scene View.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Rename the Duplicate Button as the Right button.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Create a C# Script
 
Right-click on Assets. Select Create >> C# script.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Rename the script as Movement.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Write the code like the following.
 
Double click on the Movement. The MonoDevelop-Unity editor will open up.
  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4. public class Movement: MonoBehaviour {  
  5.     //variables    
  6.     public float moveSpeed = 300;  
  7.     public GameObject character;  
  8.     private Rigidbody2D characterBody;  
  9.     private float ScreenWidth;  
  10.     // Use this for initialization    
  11.     void Start() {  
  12.         ScreenWidth = Screen.width;  
  13.         characterBody = character.GetComponent < Rigidbody2D > ();  
  14.     }  
  15.     // Update is called once per frame    
  16.     void Update() {  
  17.         int i = 0;  
  18.         //loop over every touch found    
  19.         while (i < Input.touchCount) {  
  20.             if (Input.GetTouch(i).position.x > ScreenWidth / 2) {  
  21.                 //move right    
  22.                 RunCharacter(1.0 f);  
  23.             }  
  24.             if (Input.GetTouch(i).position.x < ScreenWidth / 2) {  
  25.                 //move left    
  26.                 RunCharacter(-1.0 f);  
  27.             }  
  28.             ++i;  
  29.         }  
  30.     }  
  31.     void FixedUpdate() {  
  32.         #if UNITY_EDITOR  
  33.         RunCharacter(Input.GetAxis("Horizontal"));  
  34.         #endif  
  35.     }  
  36.     private void RunCharacter(float horizontalInput) {  
  37.         //move player    
  38.         characterBody.AddForce(new Vector2(horizontalInput * moveSpeed * Time.deltaTime, 0));  
  39.     }  
  40. }   
Save the Program.
 
Go back to the Unity window. Drag and drop the movement script onto the MainCamera.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Drag and drop the 2D character onto the Movement script.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Click on the Play button. Press the “Left touch ” and “Left Arrow ” key. The 2D character will move to the Left side.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 
Click on the Play button. Press the “Right touch ” and “Right Arrow ” key. The 2D character will move to the Right side.
 
Mobile Touch Controls - Moving A Character From Left To Right Using C# Scripts In Unity
 

Summary

 
I hope you understood how to make mobile touch controls and move a character from left to right using C# scripts in Unity.