All posts in "Unity3D"

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

Using the HTC Vive Tracker POGO Pins in Unity3D

If you have an HTC Vive Tracker, you may have already hooked it up and started tracking objects in Unity3D.  That part isn’t too hard, and it’s a lot of fun.  But you can also flip that Vive Tracker over and start using the POGO pins on the back.  The pins are pretty easy to use and give you access to the same buttons you’d have on a normal Vive wand and even include an output for haptic feedback.

HTC Vive Tracker Documentation

To get detailed info, HTC has a guide here: https://dl.vive.com/Tracker/Guideline/HTC_Vive_Tracker_Developer_Guidelines_v1.3.pdf

Video Version

Setting up the HTC Vive Tracker in Unity3D

I’ve gone over it already and just wanted to share the steps involved to get going.

First, you need your tracker in-game.  To keep it simple, we’ll use the CameraRig prefab.

Drop a [CameraRig] into an empty scene (and delete the existing maincamera).

Under the CameraRig, add an empty gameobject and name it “Tracker“.

Add the SteamVR_TrackedObject component to the “Tracker” you’ve just created.

Select the [CameraRig].

Drag the “Tracker” to the Objects array on the SteamVR_ControllerManager.

Turn on both controllers and press play.

Move the tracker around, if you see it move in-game, you’re good to move on to the next part.

Reading Vive Tracker POGO Pins

To show how to read the inputs, I’ve created this example script.  Put it in your project, then add it to the “Tracker” object.

<script src=”https://gist.github.com/unity3dcollege/6b097fb4163abf6e6d36b33ff0d48776.js”></script>

Start playing again and make sure the Tracker is still moving (remember both controllers need to be on too).

Now let’s take a look at the image from the official documentation.

If you’re not familiar with electronics, don’t worry, this one’s pretty simple.

All you need to do is make a connection from GND (Pin 2) to whichever pin you want to trigger.

You can do this with a single wire, or ideally hook it up to a switch that’s attached to your physical device.

To test this, simply touch pin 2 & pin 4 with the same wire, and you’ll see the “Trigger” field set to true.

Pins

  • 2 – Ground
  • 3 – Grip
  • 4 – Trigger
  • 5 – Trackpad
  • 6 – Menu Button

Hooking up Hardware

If you’re not sure what to use, try digging out an old electric nerf gun like this: http://amzn.to/2uDRI9b (or find a broken used one on craigslist for free/cheap)

Rip it apart and hook up the wires coming from the trigger to the tracker’s pogo pins.

There are a few different adapters out there you can order/print, like this: https://www.thingiverse.com/thing:2127180 – It’d probably be a good idea to have some sort of adapter in there to make pin access easier…

 

 

 

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