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.
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.
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
-
- public class MyScript : MonoBehaviour{
-
- void Start () {
-
- }
-
-
- void Update () {
-
- if (Input.GetKey (KeyCode.A)) {
-
- print ("You pressed the A key");
- }
-
- }
-
- }
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.