How To Add Keyboard Controls To Move An Object C#
Today we're going to take a first look at the Unity3D game development engine. This engine is pretty like shooting fish in a barrel to use and has gained a lot of popularity in recent years. It supports multiple languages (including C#, JavaScript ,Boo) .
1) The outset thing you lot will need to practise is to download and install Unity from their website.
2) Once you have installed it, you volition be asked to open or create a new project:
3) In the "Create New Project" tab, select a folder where your project volition exist created.
You lot are and so taken to the Unity interface, and shown a welcome screen. You can close the welcome screen. From the Layout drop-down carte du jour at the top-correct, select "Tall" to switch to a unlike layout.
4) Next, from the Game Object carte, select Create Other -> Cube to identify a cube into the game world.
The cube volition now be listed in the Hierarchy section - this department shows you a list of objects in your game world. You tin can likewise meet the objects in the game globe itself, in the left portion of the screen. If you click on the cube, you tin can see its properties (due east.grand. position in the game earth) in the Inspector:
The Inspector shows information nigh the game object, such as its position in the game earth. You tin move information technology around by either irresolute values in the Inspector, or dragging the coloured arrows drawn over the object in the game world.
five) Underneath the Hierarchy section, you accept a Project department. Right click on the Assets panel in the right-hand-side of the Project department and create a new C# script:
6) The script appears under Assets. Call the script "Movement". And so, double-click the script to edit it. When you do this, MonoDevelop (arranged with Unity) opens as the default editor:
vii) In the Update() method, add the post-obit lawmaking:
if (Input.GetKeyDown(KeyCode.LeftArrow)) { Vector3 position = this.transform.position; position.x--; this.transform.position = position; }
8) In Unity, you can attach scripts to objects in order to make them do stuff. In our case, we will adhere this script to the cube to exist able to move it with the arrow keys.
The "this" in a Unity script refers to the object that the script is attached to (east.g. the cube). Each object has a transform property, which contains stuff similar its position in the world. The position consists of a Vector3 which contains the x, y and z coordinates of the object in the world.
The code above simply makes the object motility left when the left pointer key is pressed, past varying the 10-coordinate of the object's position.
In MonoDevelop, press F8 to brand sure that the lawmaking compiles. Then, switch back to Unity. Elevate the Motility script onto the Cube object in the Bureaucracy section. Once you click on the Cube in the Bureaucracy section, yous should see the Movement script in the Inspector:
9) Now, Press the Play push button at the elevation of the Unity interface to start playing. The globe view changes into an interactive rendering of the game:
10) Test the game by pressing the Left Arrow key. Doing this should move the cube to the left:
Press the Play push again to stop the game. In MonoDevelop, update the lawmaking to handle movement in all four directions:
if (Input.GetKeyDown(KeyCode.LeftArrow)) { Vector3 position = this.transform.position; position.x--; this.transform.position = position; } if (Input.GetKeyDown(KeyCode.RightArrow)) { Vector3 position = this.transform.position; position.x++; this.transform.position = position; } if (Input.GetKeyDown(KeyCode.UpArrow)) { Vector3 position = this.transform.position; position.y++; this.transform.position = position; } if (Input.GetKeyDown(KeyCode.DownArrow)) { Vector3 position = this.transform.position; position.y--; this.transform.position = position; }
Printing Play again in Unity to test it. Thanks
How To Add Keyboard Controls To Move An Object C#,
Source: https://findnerd.com/list/view/Unity3D-Moving-an-Object-with-Keyboard-Input/65/
Posted by: wilsonankintly.blogspot.com
0 Response to "How To Add Keyboard Controls To Move An Object C#"
Post a Comment