Copy

Immersion through interaction

Hey! I recently posted a clip showing some plants reacting to the player. The response to that little detail was surprisingly intense and I figured it might be interesting for some of you how I achieved those. I will not explain tools or technical aspects in detail, it would just go into too much detail. There are plenty of tutorials on rigging and physics and all that stuff. The procedure is all I can explain here.  But first of all, here’s one big note:


THERE ARE BETTER WAYS THAN THIS. I am not a ‘real’ coder, and that system works for now, but I did not test it on several configurations yet.

Also, kudos to Jonas Molgaard, who created a tutorial series on youtube, covering an almost identical approach, much more in detail.
https://www.youtube.com/watch?v=wlkJkTPcVM0

The theory behind it:

Part 1:

A small procedure checks for plants within a close range of the character. If it detects a plant, it replaces the static mesh with a fully rigged dynamic plant.

Part 2:

That dynamic plant checks the distance to the player. Once the distance is large enough, it replaces itself with a static mesh again.

Preparation
We need to rig the plant model, set up the physics in Unreal and create a blueprint (with some logic later) which we can spawn.
Rigging the plant:

I roughly put some joints into my plants and rigged them. Don’t forget to create a root bone for all of the joints. For each leaf I used only one bone since I didn’t want the physical simulations to become too complex - but you can definitely play around with that, just keep an eye on your performance.

Then, with some very simple skinning (one bone per vertice, so no soft skinning in my case), I exported the plant as fbx and got it into Unreal.

Setting up the plant's physics:
We want the dynamic plant 'start' in the exact same pose as the static plant, so we need to set this up:
I added another collidor on the root/base. Turn off Gravity, set its Physics Type to Kinematic and disable any Collision Response. That way the plant doesn't 'drop' when we turn on the simulation.
For the constrains, you can pretty much experiment what feels right for you. The important part here is, that you make use of the 'Angular Motors' to get the plant back into default state after being hit by the player. Here you can play with the values and make it bouncy or stiff or whatever.
Creating a simple plant blueprint:
Create a blueprint actor, add a skeletal mesh component, add your plant and turn on 'Simulate Physics'. For collision, I use the ragdoll preset. If you place that blueprint into the world and walk through the plant, it should move accordingly.
Also, for later logic, I created 3 variables:
"FoliageActor" (Type:Actor)
"FoliageTransform" (Type: Transform)
"StaticMeshIndex" (Type: Integer)
Part 1: Checking for plants and replacing them with our dynamic PlantBlueprint
1. Checking for instanced foliage components
I used a MultiSphereTrace, but depending on your needs you could also shoot a line trace or whatever your need. Then I used a loop to make sure that everything that follows applies to each hit object. Then I break the hit results to get some info about the hit object.
2. Working with the results
You could spawn the blueprint right here already and feeding it with the appropriate values, but I created a custom event that does that for me, so don't get confused about my "SelectPhysPlantBPActor"-Node.

At first I cast to FoliageInstancedStaticMeshComponent (Object Input is HitComponent). 
Then pass over some values to my blueprint. The 'hitActor' to 'FoliageActor', the StaticMeshIndex by finding the component in the array of 'Get Components by Class' and last but not least, the InstanceTransform, so we can tell the blueprint the exact position of the static mesh.
After Spawning our Blueprint and feeding with values, I simply 'Remove Instance'.
I've had some issues with the static mesh ID in order to spawn different types of plants (which works great on landscape, but the IDs switch when spawned on static meshes), so I simply checked the 'Component Name' for certain string parts (like 'farn', or 'grass'), and decided from there which plant blueprint to spawn. Dirty workaround for now.
Part 2: Checking distance to player and replace BP with static mesh instance
Now that we can successfully get rid of the static mesh component and spawn the dynamic plant, we need to be able to switch back - otherwise the performance would suffer pretty soon.
So the previously built blueprint checks every frame how far the player is away from its center (vector minus vector = result in vectorlength, check length). As soon as the distance is larger than x, proceed.
Now, with the values we received from the spawn routine, we can simply add a new instance of the according mesh to the foliage actor and destroy itself (kill the blueprint).
Yes, it is ugly, it is not tested extensively, but .... it works. I hope you understood the basic concept and have fun exploring.
The End
Let me know if you have questions and if this was interesting or helpful.
Thank you so much for following, and as always, spread the word and tell your friends - you guys are the ones that make Omno successful.
Copyright © *2017* *StudioInkyfox*, All rights reserved.

Want to change how you receive these emails?
You can update your preferences or unsubscribe from this list.
https://playomno.us16.list-manage.com/unsubscribe?u=5f6fbc14d2e5f8942e5adf72c&id=7cb7f8f311&e=[UNIQID]&c=f69da851e9
Email Marketing Powered by Mailchimp