Layne  Fadel

Layne Fadel

1625397600

Making a RTS game #17: Introducing a sound system 2/2 (Unity/C#)

Last time, we talked about the importance of sounds in video games and how they increase the immersion of the player; we also took a look at Unity’s sound system and even “pre-optimised” our sound file imports to avoid consuming too much memory. In this second part, we’ll focus on how to actually implement the sound system. So we’re going to see how to play some background music, global and local ambient sounds and contextual sounds like unit responses.

Note: I’ve embedded some videos with sound in this article to show our progress as we code the feature, but the quality of the sound recording isn’t very high — sorry in advance!

Preliminary step: moving the listener to a “ground target”

In part one of this duo of episodes, I mentioned that we had to tinker with the default listener setup a bit. Unity assumes that, in most of scenes, the camera is the representation of the player in the virtual world, position-wise. So it makes sense to use this object as the audio listener, especially for all the 3D audio sources.

However, in our RTS game, there is actually an offset between the actual point we are looking at (the 3D point on the ground that is the projection of the 2D point in the middle of the screen) and the position of the camera (up there in the sky, looking down at the ground). Thus we need to account for this difference and rather than keeping the listener on the camera, we need to place it on the ground.

The problem is that, for now, this point is completely abstract — we don’t actually have an object in our scene at that position. So let’s add a new “GroundTarget” empty game object as a child of our camera and add an AudioListener on it:

Remember that we also need to remove the current one from the camera.

Now, we can pass a reference to this “GroundTarget” in the CameraManager class and simply update its position when we set up the camera, and whenever it later moves:

Alright, we have a “cursor” that tracks where the camera is looking. This point will be used to compute the distance to 3D audio sources, as we’ll see soon with the local ambient sounds.

#unity #development #csharp #games

What is GEEK

Buddha Community

Making a RTS game #17: Introducing a sound system 2/2 (Unity/C#)
Layne  Fadel

Layne Fadel

1625397600

Making a RTS game #17: Introducing a sound system 2/2 (Unity/C#)

Last time, we talked about the importance of sounds in video games and how they increase the immersion of the player; we also took a look at Unity’s sound system and even “pre-optimised” our sound file imports to avoid consuming too much memory. In this second part, we’ll focus on how to actually implement the sound system. So we’re going to see how to play some background music, global and local ambient sounds and contextual sounds like unit responses.

Note: I’ve embedded some videos with sound in this article to show our progress as we code the feature, but the quality of the sound recording isn’t very high — sorry in advance!

Preliminary step: moving the listener to a “ground target”

In part one of this duo of episodes, I mentioned that we had to tinker with the default listener setup a bit. Unity assumes that, in most of scenes, the camera is the representation of the player in the virtual world, position-wise. So it makes sense to use this object as the audio listener, especially for all the 3D audio sources.

However, in our RTS game, there is actually an offset between the actual point we are looking at (the 3D point on the ground that is the projection of the 2D point in the middle of the screen) and the position of the camera (up there in the sky, looking down at the ground). Thus we need to account for this difference and rather than keeping the listener on the camera, we need to place it on the ground.

The problem is that, for now, this point is completely abstract — we don’t actually have an object in our scene at that position. So let’s add a new “GroundTarget” empty game object as a child of our camera and add an AudioListener on it:

Remember that we also need to remove the current one from the camera.

Now, we can pass a reference to this “GroundTarget” in the CameraManager class and simply update its position when we set up the camera, and whenever it later moves:

Alright, we have a “cursor” that tracks where the camera is looking. This point will be used to compute the distance to 3D audio sources, as we’ll see soon with the local ambient sounds.

#unity #development #csharp #games

Abdullah  Kozey

Abdullah Kozey

1624721460

Making a RTS game #16: Introducing a sound system 1/2 (Unity/C#)

We’ve worked on quite a lot of features so far: instantiating and moving units, setting up a camera, lighting up the scene with some FOV and fog of war… there is, however, one thing we haven’t talked about yet: sounds

Since we need to prepare quite a lot of little things, I’ll split this tutorial in two parts — in this article we’ll focus more on why sounds are paramount in games, how Unity’s sound system works and how you can avoid consuming too much memory with your audio clips. Then in part 2 we’ll actually implement the system and play our audio files during the game.

Sounds in video games

Sounds are crucial for immersion — when you’re faced with a multimedia content like a movie or a video game, they are about as important as images. Over the years, video games have improved in their rendition of sounds: from the 8-bit musics we had in the 70s to the grandiose soundtrack we now have on big titles, we’ve certainly come a long way. As technology advanced, we’ve been able to include sounds that were more and more realistic, and even some spatialization effects (in particular thanks to binaural recordings).

In most video games, you can differentiate between 3 types of sounds.

The background music

Ever since the beginning, video game designers have worked on finding a unique musical identity for their creation — think of Mario’s or Sonic’s themes, or more recently of the amazing soundtrack of The Last of Us series! The musical theme will be there all along the player’s experience and it will most likely have a big influence on it: just like in films, it can increase the emotional impact of an important scene, or it can help with creating an ambiance.

Video game composers can face various challenges. They may have to make coherent soundtracks for series of games like Glen Stafford’s work at Blizzard (for the World of Warcraft and Starcraft games); or music can be an inherent part of the game as in Crypt of the NecroDancer; or it can be licensed music that is left in the control of the player by providing him/her with radios and CDs to choose from in-game…

Overall, you have to remember that with video games gaining more and more renown, their soundtracks are becoming more and more spectacular, too — but that even a lo-fi or retro 8-bit vibe tune can work, as long as it conveys the right meaning for your game.

#tutorial #development #3d #csharp #unity #rts game

Autumn  Blick

Autumn Blick

1602565700

Game Development with .NET

We’ve launched a new Game Development with .NET section on our site. It’s designed for current .NET developers to explore all the choices available to them when developing games. It’s also designed for new developers trying to learn how to use .NET by making games. We’ve also launched a new game development Learn portal for .NET filled with tutorials, videos, and documentation provided by Microsoft and others in the .NET game development community. Finally, we launched a step-by-step Unity get-started tutorial that will get you started with Unity and writing C## scripts for it in no time. We are excited to show you what .NET has to offer to you when making games. .NET is also part of Microsoft Game Stack, a comprehensive suite of tools and services just for game development.

A picture of a game controller

.NET for game developers

.NET is cross-platform. With .NET you can target over 25+ different platforms with a single code base. You can make games for, but not limited to, Windows, macOS, Linux, Android, iOS, Xbox, PlayStation, Nintendo, and mixed reality devices.

C## is the most popular programming language in game development. The wider .NET community is also big. There is no lack of expertise and support you can find from individuals and user groups, locally or online.

.NET does not just cover building your game. You can also use it to build your game’s website with ASP.NET, your mobile app using Xamarin, and even do remote rendering with Microsoft Azure. Your skills will transfer across the entire game development pipeline.

logos of some gaming platforms supported by .NET

Available game engines

The first step to developing games in .NET is to choose a game engine. You can think of engines as the frameworks and tools you use for developing your game. There are many game engines that use .NET and they differ widely. Some of the engines are commercial and some are completely royalty free and open source. I am excited to see some of them planning to adopt .NET 5 soon. Just choose the engine that better works for you and your game. Would you like to read a blog post to help you learn about .NET game engines, and which one would be best for you?

#.net #.net core #azure #c# #game development #azure #cryengine #game developers #game development #game development with .net #game engines #games #monogame #playfab #stride #unity #visual studio #waveengine

Juanita  Apio

Juanita Apio

1621083660

Making a RTS game #10: Moving the camera (Unity/C#)

In this post, we’ll talk a bit about graphical projections and camera set up, and then we’ll do some user input handling and reuse the notion of coroutines we saw last time.

Graphical projections: perspective, orthographic, axonometric…

Video games are displayed on 2D screens. So, as they started to put more and more 3D objects in our games, engineers and programmers have had to find various views of representing this 3D in a 2D space. Luckily, graphical projections (techniques used to show 3D objects in a 2D view) are hardly new stuff — scientists have been drawing complex 3D machines on paper for centuries. Over the years, plenty of ideas have come up on how to best represent the 3rd dimension in 2D: some try and reproduce the way our human eye sees things by incorporating perspective with vanishing points, others decompose the 3D object to show all of its sides separately, others try and mix the two to show as much of the object as possible while not deforming it too much…

In a RTS game, we are confronted to this question since we have 3D objects (our units, the trees and rocks on the ground, etc.). In those games, the camera is very often orthographic — this way, you get a literal bird’s eye-view of the world which helps with micro- and macro-management of your armies and production. More precisely, we usually use the isometric projection — this type of projection is a subtype of parallel orthographic axonometric projections, as shown below:

“Classification of the orthographic projection and some 3D projections”, by Cmglee — Own work, CC BY-SA 4.0, https://commons.wikimedia.org/w/index.php?curid=83384053

When computers were not as efficient as they are now, orthographic projections were an amazing way of simulating 3D for video games because they’d let artists create sprites that programmers could then paste next to each other in a neat grid — and you’d get a 3D feel. Also, you didn’t need to worry about scaling the visuals depending on the distance to the camera: your sprites had one set size and there was no need to dedicate compute power to recomputing it live. It also let the game take care of the camera for the players so they could focus on the game at hand, rather than moving both the characters and the camera.

Isometric projection in itself is interesting because it gives quite a comprehensive and well-proportioned 2D representation of our 3D objects: if you have a cube, so with edges of the same length, an isometric projection will scale them proportionally in the 2D representation, thus we still get the same equal lengths.

However, it is way harder to mentalize the directions with this rotation. If you’re going “up” in your view, then you should actually be moving along a diagonal in the world space. This will make camera movement more complex and it will become particularly cumbersome in later episodes, when we want to make a minimap (that will not be rotated 45°). So, instead, we’ll be using a pseudo-axonometric projection where we face the objects (on the left), compared to the “real” isometric projection (on the right)!

We can still simulate the isometric view (and in particular take advantage of it showing objects proportions better) by rotating the objects mesh in the scene at a 45° angle on the global Y-axis. More precisely, we’ll apply a rotation on the “Mesh” sub-object in our unit prefabs, for example for the “House” building:

This way, we get the best out of both world while reducing the mental overhead of computing the camera field of view ;)

Note: even in a true isometric projection, the vertical axis may not be scaled the same; plenty of old “isometric video games” actually used the dimetric projections, in particular to avoid pixel aliasing. Nowadays, computer graphics have improved enough for anti-aliasing to kick in spontaneously and take care of this, so we can revert to “true” isometric projections if we want. But most of the RTS games you might think of (for example the ones I cited in the first article of this series like Age of Empires, Caesar, StarCraft…) have this orthographic view that gives a unique feel to the game.

#games #csharp #tutorial #unity #game #rts

Juanita  Apio

Juanita Apio

1621149480

RTS Interlude #2: Refactoring the event system (Unity/C#)

However, this implementation is far from ideal. In particular, if we start adding more and more data types to our events, we’ll have to remember to add fields to this CustomEventData struct and update all constructors. And differentiating between “typed” and “not-typed” events was important at first to understand the event system well, but it’s a bit user-unfriendly in the long run.

Now that we’re more fluent with C## and type casting (especially thanks to our multiple glances at polymorphism and inheritance), we can improve this system and leverage C# object variable type! :)

Refactor of the EventManager class

The new class I propose for EventManager has two main differences with the previous one:

  • typed events don’t use a CustomEventData class anymore, rather we’ll be using the C# object variable type that is very “lazy” and can be casted into anything quite easily further down the road (it’s sort of like a “any type of variable is accepted here” flag ;))
  • we don’t differentiate between events and typed events anymore in our trigger/listener calls: instead, we’ll take advantage of C## function overload to automatically register the event as a typed event if some data is provided, else as a plain old (non-typed) event

#tutorial #unity #games #csharp #interlude #rts