O'Reilly logo

Learning Unity 2D Game Development by Example by Venita Pereira

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Game controls

In the previous chapter, we learned how to move a character left and right using input from our keyboard. However, many games make use of virtual controls instead, which are onscreen controls. Therefore, it is worth creating our own game controls onscreen as opposed to entering input on the keyboard.

To do this, we will make use of sprites for the HUD to display the virtual controls and raycasting to detect when the player touches a control for input.

Raycasting

Raycasting is a query on the scene that returns objects that intersect with a given ray (which is a point in space with a direction). If we cast a ray from the main 2D camera in a straight line into the screen (specified by where the player is touching/clicking), we can then ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required