♪♪♪
Zak Parrish: Hi there. Welcome to another
video training session brought to you by Epic Games. In this video,
we are going to show you how to create a hover component that you can attach
to any Static Mesh and make it hover in your scene.
Here is an example.
I am going to go into the Starter Content, and the Shapes folder.
I will drag in the Shape_QuadPyramid and drag it up into the air.
In the Details panel, change the Mobility property to Movable.
As you will see as we get going, this is an important part.
I will go into my Components folder.
Here is an example of what we are going to be building.
I am going to drag HoverOriginal onto the component list
of my selected StaticMeshComponent.
Now, all I have to do is click Play and my pyramid is hovering.
In fact, I can move it around the scene and you see it continues to hover.
Now that we know what we are going to be building,
let's get to work and make something.
Right-click in the Content Browser.
You will notice I have created a folder specifically called Components.
If you don't have this folder, you can go into your Blueprints folder,
or right-click and select New Folder.
I am going open the Components folder.
Right-click and select Blueprint Class.
We have a lot of different Parent Classes to choose from.
We want a Scene Component.
That is a component which has its own relative location, which is relative to the object.
Click Scene Component.
Call this "HoverComponent".
Once you have that, double-click HoverComponent to open it in the Blueprint Editor.
You are going to see a couple events which are disabled.
They are sitting there waiting for you to do things to them.
We have Event Begin Play and Event Tick.
We are going to start with Event Begin Play.
We need to get a reference to the main primitive component
that is applied to our object.
A primitive component is any component that we can apply physics to,
such as a Static Mesh.
Right-click in the graph, type and select "Get Owner".
The owner is the object to which our hover component has been attached.
Drag off the Return Value and type "components".
Select Get Components by Class.
We can choose the type of component class we are looking for.
Click the Select Class drop down, and search for "primitive" and select Primitive Component.
As I mentioned, a primitive component is a component
which can have physics applied to it.
That is going to return an array, which is a list of all potential
primitive components on our object.
Drag off the Return Value, type and select "Get".
Leave the Get set to 0.
That is literally the same thing as saying get whatever is first on your list
because whatever it is should be good enough.
Right-click on the pin and select Promote to Variable.
Call this variable "PrimitiveComponent".
We set it to a variable so we don't have to go through and
create all these nodes anytime we want to talk to this object.
Now we have that stored. Lastly, we want to tell the
primitive component to simulate physics. Drag off the Get Component, and type "simulate".
Select Set Simulate Physics.
Check the Simulate checkbox to set it to true.
Click Compile and click Save.
That is a good habit.
We could test this.
Here is how we are going to test it.
I am going to delete the Static Mesh from the scene.
Within StarterContent, open the Shapes folder.
Drag-and-drop Shape_Torus into the level.
The idea of a floating donut amuses me to no end.
Go to ThirdPersonBP > Blueprints > Components.
Drag-and-drop the HoverComponent onto the components list.
Remember to select the object itself, and make sure
its Mobility property is set to Movable.
You will get errors if you don't do this.
It is not a big deal.
You can press Stop, set it to Movable, and try again.
We haven't set up a hover ability right now.
As a reminder up to this point, we have only told it to simulate physics.
This is a great way to test and see if we have something started.
Click the Play Options menu and select Simulate.
The object should fall to the ground.
It does, which is perfect.
With that, we can starting setting up the hover behavior.
The hover behavior is going to happen based off of tick.
Tick is an event that is called every single frame.
On every single frame, we are going to start by looking straight down
from the location of our object.
We are going to do that with a line trace.
Drag off Event Tick and type "line trace". Select Line Trace by Channel.
We are going to use the Visibility channel.
Basically, look in the direction I tell you to
and see if you find anything visible.
We need to supply a Start and an End for this.
The Start is easy.
We can use the world location of our component. Right-click, type and select "World Location".
This is going to be the world location of the hover component itself.
Plug the Return Value into Start. Easy.
No problem.
For End, we want to look 150 units straight down from wherever we started.
We can use the same world location.
Drag off the Return Value and press - (minus key) and select Vector - Vector.
Right-click on the second pin and choose Split Struct Pin.
That is going to give you input access to X, Y, and Z,
which is really handy and one
of my favorite additions to Blueprint.
Set Z to 150.
Plug the result into End, so it becomes the end point.
As a refresher, we are drawing a line straight down 150 units
from the location of our component
relative to the object.
I want to be able to change the Z value.
Right now it is hard coded and set to 150.
Right-click on the Z pin and select Promote to Variable.
Call this "TraceLength".
I set the value first.
Click Compile and select Trace Length.
Notice its Default Value is already set to 150.
I use that trick a lot.
I will set a value and then promote it to a variable,
so I don't have to set its defaults later.
Now, we are doing our trace.
We don't want to perform any kind of hover logic
unless the trace happens to hit something.
Drag off the Return Value, type and select "Branch".
Everything else we do will be plugged into True,
and nothing will be connected to False.
We don't do anything if the trace doesn't hit anything,
which will keep this relatively simple.
What we are going to do from here involves a little bit of vector math.
Don't be frightened; it is all pretty straightforward.
Start by dragging off the Out Hit.
The Out Hit is the point in space that trace happened to hit.
Type "break" and select Break Hit Result. If we break this up,
there are all kinds of data stored in here.
We have the hit Location.
We have the Impact Normal, which is the direction that the surface we hit was facing.
We can tell what kind of material was applied to it.
We can find out all kinds of fun things if we want.
Let's start simply. We are going to take the hit location and
subtract from that. Drag off Location, type - and select Vector - Vector.
Once again, we are going to get the component's world location.
Right-click, type "world location" and select Get World Location.
Plug the Return Value into the Vector - Vector. By subtracting them,
we have theoretically drawn a line from the point we hit back to
the location of our object.
This is really just the way we are going to use to calculate the distance.
Drag off from the result of the Vector - Vector, and type "length".
Select Vector Length.
This number literally represents how far away our component is
from whatever it happened to hit.
That is going to be important because we can take the value
and divide it. Drag off the Return Value, press / and select Float / Float.
We will divide by TraceLength. Drag-and-drop TraceLength on top of the Float / Float.
This will give us a value will be between 0 and 1, for all intents and purposes.
We will achieve a value of 0 if the hit distance is 0 units away,
or basically right on top of our component's location.
As we get closer to TraceLength, we will blend out to 1.
We would have 150/150, which is 1.
Now that we have a 0 to 1 value, we are going to do a linear interpolation.
Right-click and type "linear interpolation" or just "lerp".
Under Float, select Lerp.
Plug the Float / Float result into Alpha.
Again, this node results in a 0 to 1 value.
That is perfect to drive the Alpha of a lerp.
A lerp is going to blend between two (2) values given a 0 to 1 Alpha.
When our object is hovering very close to the ground, we want a lot of force.
When it gets farther away, we want that force to go back to 0.
The value at A is 0.
This is what will happen if we are really close to the ground.
Set A to 500,000. The amount of force is half a million.
We will leave B at 0.
That means if we are more than 150 units away from the ground,
we are not applying any force at all. This is exactly what we want.
Next, we want to change the direction of our force
based off the direction of the ground.
If we drop this object while it is hovering on a ramp,
we want it to slide down the ramp.
Drag off Impact Normal, type *, and select Vector * Float. We want to multiply it.
Plug the Lerp Return Value into the Vector * Float.
Now that we have all of that, let's apply our force.
This is where things start to get really easy.
Drag-and-drop PrimitiveComponent onto the graph and select Get.
Drag off of Primitive Component and type "force".
Under Physics, select Add Force.
Yes, and may it always be with you.
Plug the True of the Branch into Add Force.
Plug the end result of the multiply that we just calculated into Force.
We could actually test this again right now.
Click Compile. Click Save, so we don't lose any of our work.
Click Play.
Check it out.
Our object is hovering.
It is hovering really quickly.
As a matter of fact, it looks like it is steadily accumulating velocity
and it is bouncing higher and higher.
This is awesome and I love it, but it is not very useful.
Double-click to open HoverComponent.
Select the Primitive Component variable and press CTRL + C to copy it.
Press CTRL + V to paste it.
We don't have to do this.
We could drag a wire out, but I am trying to keep this nice and neat.
Drag off this Primitive Component and type "linear". Select Set Linear Damping.
Plug Add Force into Set Linear Damping. Linear Damping is a
way we can slow this object down as it moves through space,
so it doesn't move as quickly.
Set In Damping to 1, which will really slow down the object.
Select the Primitive Component, copy and paste it.
Drag off of it and type "angular". Select Set Angular Damping.
Angular Damping does the same thing as Linear Damping but for rotation,
so the object can't start spinning really fast.
Set In Damping to 0.6.
Again, we could test this.
Before we test, let's wrap things up.
We have some values here that would be nice to customize,
like the amount of force we
are applying when we get really close to the ground.
It would be nice if that was variable and we could change that.
Right-click A and select Promote to Variable.
Call this "HoverForce".
We may also want to change Linear Damping.
Right-click In Damping and select Promote to Variable.
Call this "LinearDamping".
I will move that variable to make it nice and clean.
On Set Angular Damping, right-click In Damping, and select Promote to Variable.
Call this "AngularDamping".
Move the variable down.
There we are. The last thing is just for a lot of extra customizability.
Click the eye icon next to TraceLength, HoverForce, LinearDamping, and AngularDamping.
The eye icon makes all of these variables editable.
Now if we take a look at our object
and select the HoverComponent in the component list,
we have access to all of those values inside of the Details panel.
That is very cool because now we can click Play and our object is hovering.
If we select the Hover component we just created, we can start tweaking it.
We can pull our Trace Length down.
You can see the object is a lot lower to the ground.
We can lower Hover Force and you can see it is slowing down a little bit.
Increase Linear Damping so it is slowing down and not moving anymore.
Now that we have made some tweaks, we have an idea of the values we like.
I think we lost those values because we stopped simulating.
That is totally fine.
Let me test something real quick.
Select the Hover component.
Lower the Trace Length to where it is doing something like that.
Press K. Press Pause. I don't think this is going to do what I want.
It did save the values.
You can press the K key to save the values
you change while you are simulating, which is very helpful.
Be sure to use that to your advantage as well.
Lastly, we can prove this worked. Click the Play Options and choose Selected Viewport.
Run up and bounce into the object.
We can apply it to other objects as well.
Go to the Shapes folder and drag-and-drop Shape_Sphere into the level.
Move it up into the air.
Don't forget to make sure you set Mobility to Movable.
Drag-and-drop Shape_NarrowCapsule into the level.
Press E to rotate it off to the side.
Under Mobility, make sure you set it to Movable.
Go back to the Components folder. Here is our HoverComponent.
Drag-and-drop HoverComponent onto StaticMeshComponent.
Do the same thing for the sphere.
Select the sphere.
Drag-and-drop HoverComponent onto StaticMeshComponent.
Click the Play Options menu and select Simulate.
Everything is hovering. It's amazing!
There you go.
You have seen how to create a reusable hover component
that you can attach to any Static Mesh
that you want to make it hover in your scene.
That is going to wrap things up for this video.
Thank you so much for watching.
♪♪♪