Mouse Input Using C# In Unity

Introduction

 
This article demonstrates how to input through the mouse using C# scripts in Unity.
 
Prerequisites
 
Unity Environment version 5.6.1
 
Once again, refer to my previous article to get started.
  • Transforming Objects Using C# Scripts In Unity
Create a Sphere
 
Step 1
 
First, you have to open the Unity project. Create terrain, trees, and water. Add skyboxes in your project. Create C# scripts and rename the script as a move. Then create a sphere in your project.
 
 
Right-click on the Assets. Click on the "Import New Asset".
 
 
Select the brick wall image in your storage and click the "Import" button.
 
 
The brick wall image can be added to the assets. Then, drag and drop the image into a sphere. The sphere can be changed into a brick wall.
 
 
Mouse Input
 
Step 2
 
Go to the mono-development and 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.01f, 0f, 0f);      
  16.         }  
  17.         if (Input.GetKey (KeyCode.S)) {  
  18.             transform.Translate (-0.01f, 0f, 0f);      
  19.         }  
  20.         if (Input.GetKey (KeyCode.D)) {  
  21.             transform.Translate (0.0f, 0f, -0.01f);      
  22.         }  
  23.         if (Input.GetKey (KeyCode.A)) {  
  24.             transform.Translate (0.0f, 0f, 0.01f);      
  25.         }  
  26.   
  27.   
  28.         bool isLeftButtonDown = Input.GetMouseButtonDown (0);  
  29.         bool isRightButtonDown = Input.GetMouseButtonDown (1);  
  30.         bool isMiddleButtonDown = Input.GetMouseButtonDown (2);  
  31.   
  32.         print (isLeftButtonDown);  
  33.   
  34.     }  
  35.   
  36. }  
Save the program.
 
 
Go back to Unity. Click on the move script. The coding will be displayed in the Unity window.
 
 
Click on the “Play” button. False message will be displayed.
 
 
When clicking on the mouse in your game view, a "true" message will be displayed.
 
 
Write the following code to display the message on a mouse click.
  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.01f, 0f, 0f);      
  16.         }  
  17.         if (Input.GetKey (KeyCode.S)) {  
  18.             transform.Translate (-0.01f, 0f, 0f);      
  19.         }  
  20.         if (Input.GetKey (KeyCode.D)) {  
  21.             transform.Translate (0.0f, 0f, -0.01f);      
  22.         }  
  23.         if (Input.GetKey (KeyCode.A)) {  
  24.             transform.Translate (0.0f, 0f, 0.01f);      
  25.         }  
  26.         if (Input.GetMouseButtonDown (0)) {  
  27.             print ("The Left mouse button was pressed");  
  28.         }  
  29.   
  30.   
  31.     }  
  32.   
  33. }   
Save the program.
 
 
Go back to Unity. Click on the move script.
 
 
Click on the “Play” button. Nothing will be displayed in your console view. When you click on the mouse, the message will be displayed.
 
 
Click on the mouse in your game view. The message will be displayed - “The Left mouse button was pressed”
 
 
Step 3
 
Find the mouse move the axis in your game. 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.     void Start () {  
  7.     }  
  8.     void Update () {  
  9.         if (Input.GetKey (KeyCode.W)) {  
  10.             transform.Translate (0.01f, 0f, 0f);      
  11.         }  
  12.         if (Input.GetKey (KeyCode.S)) {  
  13.             transform.Translate (-0.01f, 0f, 0f);      
  14.         }  
  15.         if (Input.GetKey (KeyCode.D)) {  
  16.             transform.Translate (0.0f, 0f, -0.01f);      
  17.         }  
  18.         if (Input.GetKey (KeyCode.A)) {  
  19.             transform.Translate (0.0f, 0f, 0.01f);      
  20.         }  
  21.         if (Input.GetMouseButtonDown (0)) {  
  22.             print ("The Left mouse button was pressed");  
  23.         }  
  24.         float mouseXvalue = Input.GetAxis ("Mouse X");  
  25.         float mouseYvalue = Input.GetAxis ("Mouse Y");  
  26.   
  27.         if (mouseXvalue != 0) {  
  28.             print ("Mouse X movement: " + mouseXvalue);  
  29.         }  
  30.         if (mouseYvalue != 0) {  
  31.             print ("Mouse Y movement: " + mouseYvalue);  
  32.         }  
  33.     }  
  34. }   
  35.       
Save the program.
 
 
Move the mouse to up. The positive X, Y value will be displayed in the console view.
 
 
Move the mouse to down. The negative X, Y value will be displayed in the console view.
 
 

Summary

 
I hope you understood how to input through mouse using C# scripts in Unity.