Keyboard Input Using C# In Unity

Introduction

This article demonstrates input in unity. Players interact with the game by pressing keys in the keyboard.

Prerequisites

  • Unity Environment Version5.6.1
  • Once again, refer to my previous article to get started.
  • Scripting With C# In Unity

Keyboard Input

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 MyScript.

 
 

Open the Mono Development-unity.

 

Write the code like the following,

  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class MyScript : MonoBehaviour{  
  6.     // Use this for initialization  
  7.     void Start () {   
  8.           
  9.     }  
  10.   
  11.     // Update is called once per frame  
  12.     void Update () {  
  13.   
  14.         float horizontalValue = Input.GetAxis ("Horizontal");  
  15.         float verticalValue = Input.GetAxis ("Vertical");  
  16.   
  17.         if (horizontalValue < 0) {  
  18.             print ("Left");  
  19.         }  
  20.   
  21.         if (horizontalValue > 0) {  
  22.             print ("Right");  
  23.         }  
  24.   
  25.         if (verticalValue < 0) {  
  26.             print ("Down");  
  27.         }  
  28.   
  29.         if (verticalValue > 0) {  
  30.             print ("Up");  
  31.         }  
  32.   
  33.     }  
  34. }  

Save the program.

 

Go back to the unity window. Click on the Edit menu in the Menu bar. Click on the project settings option and select the input.

 

Click on the vertical, the up and down key W, S will be set into default.

 

The Input manager can be open. Click on the Axes, all input methods will be displayed.

Click on the horizontal, the left and right key A, D will be set into default.

 

Click on the vertical, the up and down key W, S will be set into default.

 

The all input properties can be displayed like fire, jump and etc..,

 

Player Interaction with the game

Step 2

Go to the mono development, here we can set the left, right, up and down to display the output, when you press the key W, A, S and D.

 

Go back to the unity window, Click on the “Play” button and go the console view.

 

Press the “W” key. The “Up” output will be displayed continuously.

 

Press the “S” key. The “Down” output will be displayed continuously.

 

Press the “A” key. The “Left” output will be displayed continuously.

 

Press the “D” key. The “Right” output will be displayed continuously.

 

Go to the mono development and clear the coding. Write the new code like the following.

  1. using System.Collections;  
  2. using System.Collections.Generic;  
  3. using UnityEngine;  
  4.   
  5. public class MyScript : MonoBehaviour{  
  6.     // Use this for initialization  
  7.     void Start () {   
  8.           
  9.     }  
  10.   
  11.     // Update is called once per frame  
  12.     void Update () {  
  13.   
  14.         if (Input.GetKey (KeyCode.A)) {  
  15.               
  16.             print ("You pressed the A key");  
  17.         }  
  18.   
  19.     }  
  20.   
  21. }  

Save the program.

 

Go back to the unity window and click on the “play” button. Press the A key. “You pressed the A key” output will be displayed into the console.

 

Summary

I hope, you understood how to input the keyboard using C# in unity.

Up Next
    Ebook Download
    View all
    Learn
    View all