We were set out a task in class today to “produce a detailed blog post that demonstrates a new, self-taught game or portfolio feature made in Unity and C#.” There were a handful of things I’ll need to keep in mind for this post, including:
- Have a video/GIF/Image Sequence showing the new feature in its fullest
- Show what Unity documentation I’ve used to make the feature
- Demonstrate the code that was used to make the new feature
- Explain what the code is actually doing
- Think about any further developments that can be done to take the feature further
I decided I was going to work on the highlighter script I’d attempted earlier, if only because I’d not managed to get the rendering working with the raycasting script we’d been shown a while ago. Instead, I had to put in an OnMouseOver function and use that for changing the render material.
First and foremost, I declared a couple of public variables I would need to see in the inspector for each of the items I wanted to apply this highlighter script to.
I would need to access the mesh renderer for the object, as well as tell it which was its normal colour material and which would be the highlight colour material – for the time being and just for testing, I was going to use a light grey as the highlighter colour, although it would be nice to make something a little more aesthetically pleasing for future gameplay. I also was going to include a narrative string, title myName, which would comment on the items being looked at by the player.
Overall, I wanted this script to achieve two main things: it would be used for commentary on the various things being looked at and would highlight items that the player could interact with.
Looking into the renderer via Unity’s documentation, as well as the renderer’s material documentation, it had the following information:
After looking into how the renderer works in Unity, I started piecing together the parts of script I would need to get the highlighter to work. Since I was using an OnMouseOver, I knew I needed to have a distance limit; this would ensure everything wouldn’t highlight while being looked at when the player was all the way on the other side of the room.
The float distance would be determined by the length between the door and the camera. I looked into this further via Unity’s documentation, just to be sure it was the correct calculation to use:
If the player was close enough to the object (so less than 7, in this case), it would change the material and show the myName text. However, if the player was too far away (so more than 7), then the material would remain the normalColor. There probably is a better way to write this, but it was the first thing I tried that worked so it’s been left alone for now.
However, I needed something extra for when the mouse is moved off of the highlighted item, so I added an OnMouseExit:
Since I’d decided to make my variables public at the beginning of my script, I could add the script to any item I wanted in the game and edit them through the inspector. Some items may not need a highlighter change (such as a window the player can walk up to, look out of, and a narration that appears at the bottom of the screen), but I can still use the same script.
I started with the front door, to see that everything was working correctly, before adding the script to other objects throughout the walking simulator. The script feels rather simple and does the job nicely in game: