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.
- 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.01f, 0f, 0f);
- }
- if (Input.GetKey (KeyCode.S)) {
- transform.Translate (-0.01f, 0f, 0f);
- }
- if (Input.GetKey (KeyCode.D)) {
- transform.Translate (0.0f, 0f, -0.01f);
- }
- if (Input.GetKey (KeyCode.A)) {
- transform.Translate (0.0f, 0f, 0.01f);
- }
-
-
- bool isLeftButtonDown = Input.GetMouseButtonDown (0);
- bool isRightButtonDown = Input.GetMouseButtonDown (1);
- bool isMiddleButtonDown = Input.GetMouseButtonDown (2);
-
- print (isLeftButtonDown);
-
- }
-
- }
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.
- 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.01f, 0f, 0f);
- }
- if (Input.GetKey (KeyCode.S)) {
- transform.Translate (-0.01f, 0f, 0f);
- }
- if (Input.GetKey (KeyCode.D)) {
- transform.Translate (0.0f, 0f, -0.01f);
- }
- if (Input.GetKey (KeyCode.A)) {
- transform.Translate (0.0f, 0f, 0.01f);
- }
- if (Input.GetMouseButtonDown (0)) {
- print ("The Left mouse button was pressed");
- }
-
-
- }
-
- }
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.
- 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.01f, 0f, 0f);
- }
- if (Input.GetKey (KeyCode.S)) {
- transform.Translate (-0.01f, 0f, 0f);
- }
- if (Input.GetKey (KeyCode.D)) {
- transform.Translate (0.0f, 0f, -0.01f);
- }
- if (Input.GetKey (KeyCode.A)) {
- transform.Translate (0.0f, 0f, 0.01f);
- }
- if (Input.GetMouseButtonDown (0)) {
- print ("The Left mouse button was pressed");
- }
- float mouseXvalue = Input.GetAxis ("Mouse X");
- float mouseYvalue = Input.GetAxis ("Mouse Y");
-
- if (mouseXvalue != 0) {
- print ("Mouse X movement: " + mouseXvalue);
- }
- if (mouseYvalue != 0) {
- print ("Mouse Y movement: " + mouseYvalue);
- }
- }
- }
-
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.
I hope you understood how to input through mouse using C# scripts in Unity.