• Home  / 
  • Author's archive:
About the author

Jason Weimann

Unity3D Interfaces vs Abstract Classes

By Jason Weimann / September 11, 2017

If you’re not using Interfaces and Abstract classes in your Unity projects, you’re missing out. These 2 concepts are extremely helpful for c# development, and using them properly can make your game code simple and extensible. In this article, I’ll point out the differences between the two, benefits and drawbacks, and when to use each of them.

Differences

Interface

Abstract Class

GetComponent<T>

Yes

Yes

FindObjectOfType<T>

No

Yes

Define a Contract

Yes

Yes

Default Implementation

No

Yes

Multiple on a Class

Yes

No

Vary Base Class

Yes

No

Video 1 – Interfaces

 

Video 2 – Abstract Classes

Continue reading >
Share

Replace GameObjects or Prefabs with another Prefab

By Jason Weimann / September 7, 2017

Do you have a bunch of gameobjects in your scene that you want to replace with a prefab?  Maybe you’ve been working with a placeholder object while you wait for the art & prefab to be ready?  If so, this simple editor script makes it easy.  Add this script to your Unity3D project in an Editor folder and you can quickly swap out your gameobjects with any prefab with the click of a button.

Note: I’m not sure what the original source for this script was, but I believe it was an adapted / inspired version from the scripts found on the Unity forums.  Definitely want to give credit to the forum posters here: https://forum.unity3d.com/threads/replace-game-object-with-prefab.24311/

Continue reading >
Share

Customizing the Hierarchy – Bold Prefab Text

By Jason Weimann / September 4, 2017

Customize your Scene Hierarchy with a simple editor script.  Make your prefabs bold and bright, easy to recognize.  Or extend this out further and show more detailed info in the scene hierarchy to make your workflow more streamlined.

Video

Code

Conclusions

Remember there are plenty of great editor extensions that add this kind of functionality and a whole lot more on the asset store.  If you’re interested in building your own extension, that’s awesome, but if you just want some cool extra functionality, make sure to check out those extensions as well.

Continue reading >
Share

Basics – How to load a Unity scene after a delay

By Jason Weimann / September 3, 2017

Need to load a Unity scene after a configurable delay?  This video will show you the steps required to do it easily with configurable settings in the inspector.

Video

Project Download

https://unity3dcollege.blob.core.windows.net/site/YT%Downloads/LoadSceneTimed.zip

Conclusions / Alternatives

As I mentioned in the video, you can alternatively use the pattern of StartCoroutine and use a yield return new WaitForSeconds() call to do the delay instead of a timer.  You do lose the ability to debug it in the inspector though, which is why I prefer the timer method.  Either solution will work fine to accomplish the task though.

Continue reading >
Share

Switching Control of a Character between Player & AI in Unity3D

By Jason Weimann / August 30, 2017

Do you want to allow players to take control of any character in your Unity3D game? With the right architecture, it’s not too hard to make this happen. Switch between player controlled and AI control for your characters easily by splitting out the logic and swapping them when you need. A system like this can help with sports games (taking control of a single character at once) or any other situation where you may have an NPC that you want to become player controlled.. think games where your character can charm/control enemies to fight along side them. Swapping out the enemies ‘brain’ is a simple clean solution.

Video

Conclusions

While this is a basic example of the concept, you can take it a lot further in a real game. You can build up state machines, use ScriptableObjects to define your brains, and build up a strong interchangeable system character control.

Continue reading >
Share

Unity Navigation Basics – Click to Move

By Jason Weimann / August 24, 2017

In this article I’ll explain some of the basics of the Unity Navigation System.  We’ll bake a navmesh, setup the walkable area, and show how to use the navmeshagent to control the character’s movement.

Video Version

Getting Started

To get started, I imported an asset pack, but you can use anything you want, even a simple plane.

Select the meshes in your scene that you want the character to navigate on and check the NavigationStatic box in the inspector.

Checking this box tells the navigation system that we want to have the mesh considered for navigation.

But we also want those buildings to be considered.. since they’re ‘blocking‘ the area.

So we need to set Navigation Static on any mesh that’s touching our navigable area.

Baking the Navmesh

Now open the Navigation window under Window->Navigation

On the Bake Tab, select Bake.

Making Areas Not Walkable

If you see areas like this where you don’t want the character navigating, but are blue..

You’ll need to select them and change their navmesh layer to “not walkable

This tells the system that they need to block navigation, but not be walkable themselves.

Controlling a Character

For this sample, I’ve created a capsule to be my character.

Add a NavMeshAgent to the capsule and place it near the ground.

Now we need a custom script to read input and tell the agent where to go.

Create this NavMeshClickController script and add it to the capsule.

Let’s take a look at what’s going on here..

In the Update method, we’re checking for the user to click the mouse with Fire1.

If they do, we create a ray into the scene from where the player clicked.

We take any hit point on that ray and tell the NavMeshAgent to set it’s destination.

If that point is on the NavMesh, the agent will start walking there.  If it’s not, nothing will happen.

Important Note

We must have a MainCamera for this to work.  Because we’re using Camera.main, if there’s no camera with the mainCamera tag set, we’ll have a null reference exception.

For my example, I created a camera, tagged it as mainCamera, and made it a child of the character so it would follow the character around.

Conclusions

The navigation system in Unity is really powerful and can do a whole lot more than I’ve shown here.  If you have characters that need to walk around, you should definitely consider using this system for them.  It’s not perfect for every situation, but it’s usually a great option, and always worth considering.

 

Continue reading >
Share

Unity3D Runtime Animating with the GameObjectRecorder

By Jason Weimann / August 18, 2017

Recording custom animations in Unity just got easier with the introduction of the GameObjectRecorder.  It’s in beta at the time of this writing, but I definitely recommend you give it a try.

To use the recorder, you need to instantiate a GameObjectRecorder, set the root, and bind the transform.

You’ll also need to create an empty animation clip for the object you’re recording.

Then every frame, simply call the recorder’s TakeSnapShot method, and when you’re done, save the clip using the recorder’s SaveToClip() method.

The sample code provided on the Unity forums here is a great way to get started, using it, you can get started recording your animations in just a few minutes: https://forum.unity3d.com/threads/feedbacks-for-experimental-feature-gameobjectrecorder-2017-1.467852/

Here’s a copy of that code:

Using the Sample Code

Find the gameobject you want to record, and add the HierarchyRecorder component to it.

Create an empty animation clip for your gameobject.

Assign that new empty clip to the HierarchyRecorder’s “clip” field.

Start playing, and check the record box.

Move your objects around as desired (this can be done w/ physics, in the scene view, navigation, or other code).

Uncheck the record box.

You’re done, play your animation 🙂

Video Version

Continue reading >
Share

How to adjust player size & scale in Virtual Reality with Unity3D

Adjusting player scale in VR is easy if you know what to do.  It gives you the ability to create really amazing effects and interesting gameplay.  You can make your player into a Giant.. or shrink them down to the size of a Mouse..  The technique in this article works across all devices, , , , , etc..

Scaling the Player Up and Down

To adjust the player scale, you need an empty gameobject.

In your scene, create an empty gameobject and reset it’s transform… name it “Player Root”

Move your main camera under the “Player Root” and reset the transform there as well.

Now simply scale the “Player Root”.  As you go up, your player will feel like they’re growing.  If you shrink down small, they’ll in turn be small.

Automatically Resizing the Player

In some situations, you may have a need to automatically resize the player so their point of view is consistent.  If your game requires a certain height to be playable, this may be a solution that can expand your audience to shorter or taller people.  You can use a script similar to the one below to adjust the players scale based on their height at the time it runs.  Ideally, you probably want to run this while they’re loading a scene or give the player a ‘re-orient’ option to allow them to adjust for their size.

Video Version

Conclusions

Resizing the player opens up a variety of fun gameplay opportunities.  If you haven’t tried it before, jump in and play around.. new ideas may come up and you could build the next amazing experience using this simple technique.

 

Continue reading >
Share

Unity3D .net 4.6 String Interpolation and Null Conditional Operator

By Jason Weimann / August 16, 2017

With the release of Unity 2017.1, the .net framework update to 4.6 has been included and is now just about ready for mainstream usage.  The .net framework upgrade is actually pretty big, it contains a lot of things under the hood, and quite a few new pieces of functionality you can use.  In this post, I’m only going to cover two of them, they’re two of my favorite.  That’s because they’re extremely easy to start using right away and will make your code just a tad bit cleaner by removing some of the extra ceremony that was required before them.  These features are String Interpolation (using the $ operator) and the Null Conditional Operator (aka Elvis operator ?. )

Important Warning

Before you upgrade to .net 4.6, be sure to commit / backup your project.  The last time I did it, I ended up with quite a few prefab references becoming null.  I don’t know if that’s a fixed issue, was a 1 time occurrence, or something else, but treat it just like you would a major version upgrade in Unity.  Commit to source control and save yourself from possible headaches.

 

To demonstrate these, I plucked a line of code from one of my real projects that happens to use them both, for a very common use.. logging.

Before I show them off though, I want you to take a quick look at this

Here, I’m writing to the logs when the character handling the puck has changed (this is a hockey game).  I log out the character that’s handling the puck change at {0} and the character that now is holding the puck with {1}.  Sometimes the puck is held by nobody though, so I need to do a null check and pass in an empty string if that’s the case..  so I’m using the ternary / ? operator.  In all, it’s 3 lines (sure it could be one long one).. and if you’re reading it for the first time, it takes a second.. or a few..  to look at, see what’s going on, make sure that ternary is correct.

Now let’s compare that to the .net 4.6 version.

The first thing to note is we no-longer have string.Format, but instead we start the string with a $ before it.  This tells the compiler that we’ll be putting variables in our string using the { }’s.  In-fact, right after the quote, we start with the first parameter.. and instead of a {0} that we have to look to the end for, we can put the variable right there.  Behind the scenes, the compiler will treat this just like a string.Format from the first version, but we don’t have to mentally track the {0} to the first parameter and so on.

We do the same for the second parameter, but there’s a little something extra.  In the 2nd parameter, we needed to check to make sure the heldBy wasn’t null.  With the Null Conditional Operator, we can tell the compiler to do that for us.  If the heldBy variable is null, it’ll be a null string.  If it’s not, it’ll evaluate the gameObject and then the .name of that gameObject.  All without us writing extra null checks.

It’s also worth noting that the Null Conditional Operator works for executing methods… or invoking events.  You could have a method named Shoot() on a character, and call it with character?.Shoot() to have the code shoot if a character is assigned to the variable..  Not the best example, but hopefully you get the idea 🙂

 Video Version

Continue reading >
Share

How to make Vive Trackers work without Controllers

A common issue I’ve seen people run into with the HTC Vive Trackers is that they can’t track them without the controllers on.  This is caused by a single line of code in the SteamVR_ControllerManager.cs file and is easy to change.  All you need to do is find the section shown below and comment out the 2nd line there.  As of the time this is written, that line # is 255 (it may change with steamvr updates).

 

Video Version

Continue reading >
Share
1 8 9 10 11 12 21
Page 10 of 21