Most of the time I only do small blueprint projects in Ue4, just because its fun, yesterday I thought it would be great to try making health orbs. The kind that fly towards you if you come close enough. So I did, you can see the result in the youtube video below.
Anyways, here we go 😉 First we need to create an actor, that is the MasterOrb. It will contain the things we need to make the orbs collide with the player and fly towards the player. It will also contain a function so that when you make a child actor, you can set up the function of that particular orb (Should it heal? Should it give experience points?)
I have 3 collisions, the first is a sphere collision that only blocks world static. This collision simulates physics. It is also the root. I want it to sort of jump out once you kill a pawn. Then I have a orb sphere collision, this is the collision that overlaps with the player character. When it get close enough to the player it gets destroyed. The third collision is the orb range collision. Its a big sphere and it detects if the player is close enough for the orb to be attracted to the player.
Lastly, we need the sphere itself. The visible orb. Its just a sphere with an emissive material on it. Explained later. No collision is needed on this.
Lets start there, with the orb range collision. Add a begin overlap, and then check if it is the player character that is the one overlapping (or else the bots will be able to pick up the orbs :P, we don’t want that). After you check if it was the player, you need put a “do once” node, this action is something we dont want to repeat, it should only fire once. I also added a delay, so that it takes a little time for the orbs to start moving. Then I made a custom event called “MovesToPlayerCharacter” and plugged that in at the end.
In “MovesToPlayerCharacter” custom event I first added a actor input. I again check if it is indeed the player character that is trying to get the orb. I then collect where in world space my root collision is, the orb physics we first made.
Then I added a timeline with a float value. The float ends up at 1 in range and 1.5 in time. Here you can have some fun, this adjust the way the orbs is attracted to the player, how much time they use to fly, etc. Then we are going to set the orbs world location trough the update from the timeline, and its important that we use a lerp. We hook the orbs original world location into A and the characters world position into B.
Add a set actor location at the end and you are good to go. So if you test now. What should happen is that the orbs should follow you, but wait, we forgot something important! The collisions is not set.
- Orb physics should only block world static.
- Orb range should only be overlapped by player.
- Orb sphere collision should only be overlapped by player.
- Player should detect overlap from orbs.
- World static should block orbs.
Now if you put the orb into world and go close to it the range sphere should trigger and should run the custom event and fly towards the player. However, the orb does not do anything yet and it is not destroyed.
Now we need to add an overlap to the orb collision sphere. A smaller sphere about the size of the orb itself. (Same as we talked about earlier)
When this collides with the player you again need to check if it is indeed the player (again we don’t want bots to pick up the orb). Then i made a custom event called “FunctionOfOrb”, this custom event we will call from a child actor to tell the orb what it should do once it reaches the player. Should it heal, give experience points, or stamina? I will show how I set it up in my child actor.
After we have called the custom event we are going to destroy the actor. That’s it, now the orb disappears when it reaches the player.
Now the material, its just a simple material with 2 parameters, one for colour and one for emissive strength. Create an instance of this.
In our orb master actor create a linear color variable. Set it to editable. Put the material instance on the orb sphere mesh. Go to construction script and set up the linear color variable to interact with the material. Now you can adjust the emissive color trough the child actor. Easy 🙂 I set the linear color variable to be purple on the master orb just so I know which one is the master.
So now we need to make a child actor. I made three. Right click the master and find “create child actor”. Name them appropriately.
Go into blueprints of the child and call the custom event we made earlier. Here you can call an event at the character. In my case i called a custom event I made called give health. This is an event in my player character.
The last thing I did was to create another actor, an orb spawner. What this does, when it begins to play, is to it spawns a random number of orbs, and it chooses randomly if it is a health orb or a stamina orb.
You can set it up to what you want. 🙂